【发布时间】:2019-02-06 09:52:57
【问题描述】:
我一直在尝试使用循环和数组来解决问题,并将下面的示例放在一起,该示例从工作表上的表中获取工作表名称并将它们存储在数组中,另一个循环从中运行以在单元格中添加值这些命名电子表格中的 A1 基于活动表上的单元格 D1 中的值。
我不断收到运行时错误,但我无法确定代码正在查找的值是什么,并且一直跳闸。
错误似乎位于这一行:
Sheets(myArray(x)).Range("A1").Value = EntryValue
非常感谢您对我未正确完成的事情的任何帮助。
谢谢。
代码如下:
Sub WorksheetListLoop()
Dim myArray() As Variant
Dim EntryValue As String
Dim ListRange As Range
Dim cell As Range
Dim x As Long
'Set the values to go into range
Set ListRange = ActiveSheet.ListObjects("tblArrayList").DataBodyRange
'Resize array prior to loading data
ReDim myArray(ListRange.Cells.Count)
'Loop through each cell in range and store sheetname in array
For Each cell In ListRange.Cells
myArray(x) = cell.Value
x = x + 1
Next cell
'Use the value in this cell to put into the sheets in the array
EntryValue = ActiveSheet.Range("D1").Value
'Loop through list and add value to cell
For x = LBound(myArray) To UBound(myArray)
Sheets(myArray(x)).Range("A1").Value = EntryValue
Next x
End Sub
【问题讨论】:
-
这是错误的 ActiveSheet("Sheet4") 你是说 Activesheet 还是 Sheet4?
-
尝试通过放置 on error 子句并在错误子句中放置断点来捕获错误。使用 resume 命令,光标将回到原来的行。