Excel 2010 VBA: How to store an array of worksheets as a variable?

前端 未结 5 817
忘掉有多难
忘掉有多难 2021-01-03 10:37

I am trying to have several arrays of my worksheets that I can call up in my code using.

ThisWorkbook.Sheets(Array(\"Sheet1\", \"Sheet3\"))
ThisWorkbook.Shee         


        
5条回答
  •  悲哀的现实
    2021-01-03 10:44

    I had a similar problem trying to create a dynamic array (not knowing how many sheets there was for me to deal with). I simply used this:

    Sub copyArrayOfSheets()
    
    Dim loopArray() As Variant
    
    ReDim Preserve loopArray(1 To 1)
    loopArray(1) = "Sheet1" ' a Sheet I know I need to export
    
    j = 1
    For Each loopSheet In ThisWorkbook.Sheets
        If loopSheet.Name <> "Sheet1" Then
            theName = loopSheet.Name
            j = j + 1
            ReDim Preserve loopArray(1 To j)
            loopArray(j) = theName ' Assign the name of the sheets to j-th position of loopArray() 
        End If
    Next loopSheet
    
    Sheets(loopArray()).Copy
    
    Set newBook = ActiveWorkbook    
    newBook.Activate
    
    End Sub
    

    Hope this helps in any way...

提交回复
热议问题