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

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

提交回复
热议问题