簡體   English   中英

遍歷String數組並將值分配給Worksheet

[英]Iterate through a String array and assign the value to Worksheet

我對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

基本上,我從表單的列表框中將值放入數組。 數組定義為變量類型,數組索引也定義為變量類型。

謝謝你的幫助

在以下情況下,您的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM