Sub test()
Dim Relevant_Sheets As Variant
Set Relevant_Sheets = Sheets(Array("btw.facturen", "btw.filelist", "btw.controle", "intervat", "voorblad", "medecontractanten", "Vak 01", "Vak 03", "Vak 45", "Vak 54", "Vak 55", "Vak 56", "Vak 59", "Vak 81", "Vak 82", "Vak 83", "Vak 84", "Vak 85", "Vak 86", "Vak 88"))
Number_of_Relevant_Sheets = 20
End Sub
这是我的代码的摘录。之后Relevant_Sheets
被多次使用,我使用它以便我只需要设置一次使用哪些页面。
我希望Number_of_Relevant_Sheets
计算Relevant_Sheets
(在这种情况下为20)中的工作表数量,以便在添加或删除工作表时不必手动更改工作表从列表中。
如果有人能指出我正确的方向?
答案 0 :(得分:1)
您可以使用Relevant_Sheets.Count
代替Number_of_Relevant_Sheets
答案 1 :(得分:0)
也许我去了一点"矫枉过正"在这里,但我会使用类似下面的代码(所以你可以在代码中使用所有变量和对象):
Option Explicit
Sub test()
Dim Relevant_Sheet() As Worksheet
Dim SheetNameArr As Variant
Dim Number_of_Relevant_Sheets As Long
Dim i As Long
SheetNameArr = Array("btw.facturen", "btw.filelist", "btw.controle", "intervat", "voorblad", "medecontractanten", "Vak 01", "Vak 03", "Vak 45", "Vak 54", "Vak 55", "Vak 56", "Vak 59", "Vak 81", "Vak 82", "Vak 83", "Vak 84", "Vak 85", "Vak 86", "Vak 88")
Number_of_Relevant_Sheets = UBound(SheetNameArr) + 1 '<-- add 1 because the array start at 0
ReDim Relevant_Sheet(0 To UBound(SheetNameArr)) ' <-- resize worksheet array
For i = 0 To UBound(SheetNameArr) ' <-- loop though all elements in the array, and set the relevant sheet
Set Relevant_Sheet(i) = Sheets(SheetNameArr(i))
Next i
End Sub