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

前端 未结 5 782
忘掉有多难
忘掉有多难 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...

    0 讨论(0)
  • 2021-01-03 10:48

    I also was trying to do this but I found another way

    What i was trying to accomplish was that I have a workbook with multiple sheets and gave them a name. I wanted to select a few sheets and exclude a few sheets that needed to be exported to a different excel file.

    Here is (after a lot of searching and trying) my code

    Dustin

    Dim ii As Integer 'Counter of worksheets
    Dim namefile as string 'Variable for name of the new file
    namefile = "NameOfNewFile.xlsx" 'Name of new file
    
    For ii = 1 To ThisWorkbook.Sheets.Count 'Counts from 1 to last sheetnumber
        If Sheets(ii).Name <> "Namesheet1" Then If Sheets(ii).Name <> "Namesheet2" Then Sheets(ii).Select Replace:=False 
        'NameSheet1 and NameSheet2 are being exluded from the new file
    Next ii
    
    ActiveWindow.SelectedSheets.Copy 'Copies the selected files
    
    Set NewWb = ActiveWorkbook
    NewWb.SaveAs Filename:= _
    "C:\Users\" & Environ("UserName") & "\Desktop\" & namefile, FileFormat:=xlOpenXMLWorkbook 
    'Saves as xlsx file to desktop
    NewWb.Close 'Closes the new file
    Set NewWb = Nothing 'Clear NewWb to reduce memory usage
    
    0 讨论(0)
  • 2021-01-03 11:06

    Here is an example of how arrays in VBA work:

    Sub Example()
        Dim ArrayOne() As String
        Dim ArrayTwo() As String
        Dim ArrayThree As Variant
        Dim i As Long
    
        ReDim ArrayOne(1 To Sheets.Count)
        ReDim ArrayTwo(1 To 2)
    
        For i = 1 To Sheets.Count
            ArrayOne(i) = Sheets(i).Name
        Next
    
        ArrayTwo(1) = "Sheet1"
        ArrayTwo(2) = "Sheet2"
    
        ArrayThree = Array("Sheet1", "Sheet3")
    End Sub
    

    Now from what I understand you do not want to use arrays. You can reference worksheets in your workbook like this:

    Sheets("SheetName") 'SheetName is the name of your sheet
    Sheets(1)           '1 = sheet index
    

    One way to copy sheets to a new workbook to be saved is:

    Sub Example()
        Dim wkbk As Workbook
    
        ThisWorkbook.Sheets("Sheet1").Copy
        Set wkbk = ActiveWorkbook
        ThisWorkbook.Sheets("Sheet3").Copy After:=wkbk.Sheets(wkbk.Sheets.Count)
    
        wkbk.SaveAs FileName:="C:\New Excel Book.xlsx", _
                    FileFormat:=xlOpenXMLWorkbook
        wkbk.Close
    End Sub
    
    0 讨论(0)
  • 2021-01-03 11:07

    Following Arthur's solution (last comment), I had a similar problem (thus reached this post) : I was trying to create a dynamic array, which would save a series of sheets within a Workbook in an array and then perform specific actions with that array.

    What is different is that, the user defines the sheets' names within a range (column) in excel (they represent scenarios for another macro), however this range may be expanded or shortened.

    I use 2 arrays, where i run the loop in the first and save the extension each time to the other array (for transparency reasons). Code:

    Sub testArray()
        Dim a, b As Integer
        scenarios_number = Sheets(sheet1).[c1] - 1 ' (this is where i put the # of scenarios / sheets (-1 is used as i want the array to start from 0))
        a = 0
        Dim Scenarios_array, dimension_array() As Variant
        ReDim Scenarios_array(0 To scenarios_number) '(resize array to match the #'s of scenarios)
        ReDim dimension_array(0 To a)
        For a = 0 To scenarios_number
        Scenarios_array(a) = Range("c8").Offset(a, 0).Value '(this is where my scenarios' names start within sheet1 -- using offset for the loop -- this is why i use -1 above as i want a to start @ 0)
        ReDim Preserve dimension_array(0 To a) ' (expand dimension of 2nd array)
        dimension_array(a) = Scenarios_array(a) ' (save the value in the second array, expaning its dimensions)
        Next
        MsgBox "Get Ready"
        Sheets(dimension_array()).Select
        ActiveWindow.SelectedSheets.Delete
    End Sub
    

    Hope that helps :)

    0 讨论(0)
  • 2021-01-03 11:08

    Try using the record macro functionality. It will allow you to select multiple sheets and then copy them into a new book. Next save that book and you are there. Now tinker with the code to get it to work specifically the way you want.

    It will come down to:

    ThisWorkbook.Sheets(Array("Sheet1", "Sheet3")).Copy
    ActiveWorkbook.SaveAs ...
    

    If you want to predefine the arrays, thats is easily done as well; those will just have to contain the names of the sheets. An Array can be created by using a Variable variable:

    Dim ArrayOne as Variant
    ArrayOne = Array("Sheet1", "Sheet3")
    

    And use that in the .Sheets().Copy :

    ThisWorkbook.Sheets(ArrayOne).Copy
    
    0 讨论(0)
提交回复
热议问题