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

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

提交回复
热议问题