[英]How to dynamically add elements in an array inside a loop
I have a workbook with several sheets and I need to create an array with only the wanted sheets.我有一个包含几张工作表的工作簿,我需要创建一个仅包含所需工作表的数组。 So I have this code that skips some hardcoded sheet names, but I don't know how to add the wanted sheets in my array 'sheets_names_array'.
所以我有这段代码跳过了一些硬编码的工作表名称,但我不知道如何在我的数组“sheets_names_array”中添加想要的工作表。 I have this code:
我有这个代码:
' make an array with the sheet names we want to parse
Dim sheets_names_array() As Variant, sheet As Variant
For Each ws In ThisWorkbook.Worksheets
Select Case ws.Name
Case "Qlik Ingestion"
'Do nothing
Case "Dropdown Values"
'Do nothing
Case "VBmacro"
'Do nothing
Case Else
'MsgBox ws.Name
sheets_names_array.Add (ws.Name)
End Select
Next
But the 'Add' method doesnt work.但是“添加”方法不起作用。 Do you know how to solve this please?
请问你知道如何解决这个问题吗? I have seen documentation that uses ReDim but I am not sure how to loop through the elements of the 'sheets_names_array' table
我看过使用 ReDim 的文档,但我不确定如何遍历“sheets_names_array”表的元素
You can use a collection instead您可以改用集合
Dim sheets_names_col As New Collection
and add your items like并添加您的项目,例如
sheets_names_col.Add ws.Name
Dim sheets_names_col() As New Collection
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Select Case ws.Name
Case "Qlik Ingestion", "Dropdown Values", "VBmacro"
' do nothing
Case Else
sheets_names_col.Add ws.Name
End Select
Next ws
And you can loop it like你可以像这样循环它
Dim sheet As Variant
For Each sheet In sheets_names_col
Debug.Print sheet
Next sheet
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.