[英]Iterate through a String array and assign the value to Worksheet
I am fairly new to VBA, so pardon for any silly mistakes. 我对VBA还是很陌生,所以请原谅任何愚蠢的错误。
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 当我打印listBoxValuesArray(arrayIndex)
我正在获取值
Here is the complete for loop 这是完整的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
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: 在以下情况下,您的Set命令对我来说很好用:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.