Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 2010 VBA:如何将工作表数组存储为变量?_Vba_Excel_Excel 2010 - Fatal编程技术网

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

Excel 2010 VBA:如何将工作表数组存储为变量?,vba,excel,excel-2010,Vba,Excel,Excel 2010,我正在尝试使用多个工作表数组在代码中调用它们 ThisWorkbook.Sheets(Array("Sheet1", "Sheet3")) ThisWorkbook.Sheets(Array("Sheet2", "Sheet5")) 我想知道是否有必要设置一个类似于以下内容的变量: Dim ArrayOne As String Dim ArrayTwo As String ArrayOne = ThisWorkbook.Sheets(Array("Sheet1", "Sheet3")) Ar

我正在尝试使用多个工作表数组在代码中调用它们

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 

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

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

这将归结为:

ThisWorkbook.Sheets(Array("Sheet1", "Sheet3")).Copy
ActiveWorkbook.SaveAs ...
如果你想预定义数组,那也很容易做到;这些只需包含工作表的名称即可。可以使用变量创建数组:

Dim ArrayOne as Variant
ArrayOne = Array("Sheet1", "Sheet3")
并在
.Sheets()中使用它。复制

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

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

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
Sub-copyrarayofsheets()
Dim loopArray()作为变量
ReDim保留循环数组(1对1)
loopArray(1)=“Sheet1”'我知道需要导出的图纸
j=1
用于此工作簿中的每个循环工作表。工作表
如果是loopSheet.Name“Sheet1”,则
theName=loopSheet.Name
j=j+1
ReDim保留循环数组(1到j)
loopArray(j)=theName'将图纸的名称指定给loopArray()的第j个位置
如果结束
下一页
工作表(loopArray())。复制
设置newBook=ActiveWorkbook
新书本,激活
端接头

希望这能有所帮助……

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

不同的是,用户在excel中的某个范围(列)内定义图纸的名称(它们代表另一个宏的场景),但此范围可能会被扩展或缩短

我使用2个数组,在第一个数组中运行循环,每次都将扩展保存到另一个数组中(出于透明度原因)。代码:


希望这能有所帮助:)

我也试着这么做,但我找到了另一种方法

我试图实现的是,我有一个包含多张工作表的工作簿,并给它们命名。我想选择一些工作表并排除一些需要导出到其他excel文件的工作表

这是(经过多次搜索和尝试后)我的代码

达斯汀

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
Dim ii作为工作表的整数计数器
Dim namefile as string变量,用于新文件的名称
namefile=“NameOfNewFile.xlsx”'新文件的名称
对于ii=1到ThisWorkbook.Sheets.Count,计数从1到最后一张图纸编号
如果是工作表(ii),则命名为“名称表1”,如果是工作表(ii),则命名为“名称表2”,然后命名为工作表(ii)。选择替换:=False
'名称表1和名称表2将从新文件中排除
下一个ii
ActiveWindow.SelectedSheets.Copy'复制所选文件
设置NewWb=ActiveWorkbook
NewWb.SaveAs文件名:=_
“C:\Users\”环境(“用户名”)和“\Desktop\”名称文件,文件格式:=xlOpenXMLWorkbook
'另存为xlsx文件到桌面
NewWb.Close'关闭新文件
设置NewWb=Nothing清除NewWb以减少内存使用

数组不是字符串类型,您所指的不是名称,而是图纸本身(即图纸对象),这实际上无法计算。问题是要存储或引用什么,是名称还是图纸对象?图纸对象本身。我希望能够将我在代码中选择的工作表保存为新文件。这正是我想知道的!非常感谢你,K_B!!K_B,我在将ArrayOne设置为变量时遇到了一些问题。我收到一个错误,显示“未定义用户定义的类型”。有什么想法吗?另外,谢谢你对我的耐心——我知道很明显我是新来的。不过,我必须使用“变量”而不是“变量”!再次感谢您的帮助!!啊,我真傻,的确如此。我背了一些部分,所以没有测试。谢谢你的解释!这真的帮助了我对vba的理解!然而,K_B的答案对我来说同样可行,因此我将坚持他的答案。这对我很有帮助,因为我似乎会随着时间的推移添加更多的报告,这使得添加逻辑以扩展阵列变得很容易。我更喜欢在最后增加计数器,因此我将j初始化为2而不是1。
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