Excel 2010 VBA:如何将工作表数组作为variables存储?

我正在尝试使用我的代码中可以调用的工作表的几个数组。

ThisWorkbook.Sheets(Array("Sheet1", "Sheet3")) ThisWorkbook.Sheets(Array("Sheet2", "Sheet5")) 

我想知道是否有设置类似于以下的variables:

 Dim ArrayOne As String Dim ArrayTwo As String ArrayOne = ThisWorkbook.Sheets(Array("Sheet1", "Sheet3")) ArrayTwo = ThisWorkbook.Sheets(Array("Sheet2", "Sheet5")) ArrayOne 'Call this Array then save Filename:="C:\Data\testfile.xls", FileFormat:= _ xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _, CreateBackup:=False ArrayTwo 'Call this array then save Filename:="C:\Data\testfile.xls", FileFormat:= _ xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _, CreateBackup:=False 

请让我知道,如果你能帮助我!

尝试使用loggingmacrosfunction。 它将允许您select多个工作表,然后将其复制到一本新书中。 接下来保存这本书,你在那里。 现在修改代码,使其以您想要的方式工作。

它会归结为:

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

如果你想预先定义数组,那很容易做到; 那些将只需要包含工作表的名称。 可以使用Variablevariables创build一个数组:

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

并在.Sheets().Copy

 ThisWorkbook.Sheets(ArrayOne).Copy 

下面是VBA中的数组如何工作的一个例子:

 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 

现在从我的理解你不想使用数组。 您可以像这样在工作簿中引用工作表:

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

将工作表复制到要保存的新工作簿的一种方法是:

 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 

我有一个类似的问题,试图创build一个dynamic数组(不知道有多less张我需要处理)。 我只是用这个:

 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 

希望这有助于以任何方式…

在Arthur的解决scheme(最后一条评论)之后,我遇到了一个类似的问题(从而达到了这个post):我试图创build一个dynamic数组,它将在一个数组的工作簿中保存一系列工作表,然后对该数组执行特定的操作。

不同的是,用户在excel的一个范围(列)内定义表单名称(它们代表另一个macros的情况),但是这个范围可以被扩展或缩短。

我使用2个数组,其中我在第一个运行循环,每次保存扩展到另一个数组(为了透明的原因)。 码:

 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 

希望帮助:)

我也试图做到这一点,但我find了另一种方式

我试图完成的是,我有一个工作簿与多个表,并给他们一个名字。 我想select几张纸,并排除一些需要导出到另一个excel文件的纸张。

这是(经过大量的search和尝试)我的代码

达斯汀

 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