创建自定义工作表集合的最快方法

时间:2014-07-16 07:45:46

标签: excel vba excel-vba

是否有更紧凑的编码方式(更少的行)来定义Excel VBA中的自定义工作表对象集合(除了只有很多单个.add语句):

   Sub Create_Custom_Worksheets_Collection()
     Dim myWorksheets As New Collection
     Dim i As Long, wks As Worksheet
     Dim mySheet_Names()

     mySheet_Names = Array("Sheet2", "Sheet5", "Sheet6")
     For i = LBound(mySheet_Names) To UBound(mySheet_Names)
        myWorksheets.Add Worksheets(mySheet_Names(i))
     Next i

     For Each wks In myWorksheets
        Debug.Print wks.Name
     Next wks
End Sub

自己找到一个更短的方式

Sub Shortest_Code()
   Dim wks As Worksheet
   For Each wks In Sheets(Array("Sheet2", "Sheet5", "Sheet6"))
      Debug.Print wks.Name
   Next wks
End Sub

并使用特定的集合对象..

Sub Create_Custom_Worksheets_Collection()
   Dim myWorksheets As Sheets
   Dim wks As Worksheet
   Set myWorksheets = Sheets(Array("Sheet2", "Sheet5", "Sheet6"))
   For Each wks In myWorksheets
      Debug.Print wks.Name
   Next wks
End Sub

0 个答案:

没有答案