我对VBA很新,所以请原谅任何愚蠢的错误。
我在数组中有我的工作表的名称。我试图通过使用以下内容转到数组中的每个工作表:
Set ws = Worksheets(listBoxValuesArray(arrayIndex))
但是我收到错误消息“下标超出范围”
我也试过这个:
Sheets.Select (listBoxValuesArray(arrayIndex))
当我打印listBoxValuesArray(arrayIndex)
时,我得到了值
这是完整的for循环
Dim arraySize As Integer
arraySize = jobSheetsDisplay.ListCount
Dim listBoxValuesArray() As Variant
Dim listBoxArrayIndex As Integer
Dim arrayValue As Variant
Dim listBoxIndex As Integer
Dim arrayIndex As Integer
Dim ws As Worksheet
ReDim listBoxValuesArray(arraySize)
listBoxArrayIndex = 0
For listBoxIndex = 0 To jobSheetsDisplay.ListCount - 1
If jobSheetsDisplay.Selected(listBoxIndex) Then
listBoxValuesArray(listBoxArrayIndex) = CStr(jobSheetsDisplay.List(listBoxIndex))
listBoxArrayIndex = listBoxArrayIndex + 1
End If
Next listBoxIndex
For arrayIndex = 0 To UBound(listBoxValuesArray)
Set ws = Worksheets(listBoxValuesArray(arrayIndex)) '
MsgBox (listBoxValuesArray(arrayIndex))
Next arrayIndex
基本上,我从表单中的列表框中获取值到数组中。该数组被定义为变体类型,因此数组索引也是如此。
感谢您的帮助
答案 0 :(得分:0)
在这种情况下,您的设置命令可以正常使用:
Sub dural()
Dim listBoxValuesArray(1 To 3) As String
For i = 1 To 3
listBoxValuesArray(i) = Worksheets(i).Name
Next i
arrayIndex = 2
Set ws = Worksheets(listBoxValuesArray(arrayIndex))
End Sub