I am fairly new to VBA, so pardon for any silly mistakes.
I have the names of my worksheets in an array. I am trying to go to each of the worksheets in the array by using the following:
Set ws = Worksheets(listBoxValuesArray(arrayIndex))
But I get the error message saying "Subscript out of Range"
I also tried this:
Sheets.Select (listBoxValuesArray(arrayIndex))
When I print the listBoxValuesArray(arrayIndex)
I am getting the values
Here is the complete for loop
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
Basically, I get the values into the array from a list box in a form. The array is defined as a variant type and so is the array index.
Thanks for any help
Your Set command works fine for me in this context:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.