3

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

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

我想知道是否有任何方法可以设置类似于以下内容的变量:

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 

如果你能帮助我,请告诉我!!

4

5 回答 5

4

尝试使用录制宏功能。它将允许您选择多张纸,然后将它们复制到一本新书中。接下来保存那本书,你就在那里。现在修改代码以使其以您想要的方式专门工作。

它将归结为:

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

如果您想预定义数组,那也很容易完成;那些只需要包含工作表的名称。可以使用 Variable 变量创建数组:

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

并在.Sheets().Copy

ThisWorkbook.Sheets(ArrayOne).Copy
于 2013-06-18T13:00:08.100 回答
4

以下是 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
于 2013-06-18T13:12:39.690 回答
2

我在尝试创建一个动态数组时遇到了类似的问题(不知道我要处理多少张表)。我只是用这个:

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

希望这对您有任何帮助...

于 2014-04-11T13:01:46.797 回答
0

遵循亚瑟的解决方案(最后评论),我遇到了类似的问题(因此到达了这篇文章):我试图创建一个动态数组,它将工作簿中的一系列工作表保存在一个数组中,然后使用该数组执行特定操作.

不同的是,用户在 excel 中定义一个范围(列)内的工作表名称(它们代表另一个宏的场景),但是这个范围可以扩大或缩短。

我使用 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

希望有帮助:)

于 2014-07-20T12:57:04.520 回答
0

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
于 2015-09-11T14:36:05.820 回答