发布之前,我找不到特定的答案。
有人可以协助调整Excel中现有的VBA代码,使我能够复制2张纸,而不仅仅是1张纸吗?我收到一个错误。它只能用1张纸,但不能用2张纸。我将代码以粗体显示在失败的地方。
Sub Export()
Dim FlSv As Variant
Dim MyFile As String
Dim MyTemplate As String
Dim sh As Worksheet
Dim wbNew As Workbook
With Application
.ScreenUpdating = False
.DisplayAlerts = False
End With
这是失败的地方:
Set sh = ActiveWorkbook.Sheets("Sheet 1", "Sheet 2")
sh.Copy
Set wbNew = ActiveWorkbook
MyFile = Replace("Consolidated", ".xlsm", "")
FlSv = Application.GetSaveAsFilename(MyFile, fileFilter:="Excel Files (*.xlsx), *.xlsx)", Title:="Enter your file name")
wbNew.SaveAs FlSv, FileFormat:=51
wbNew.Close
For Each s In ActiveWorkbook.Sheets
If s.Name Like "*Sheet 1*" Then
Application.DisplayAlerts = False
s.Delete
Application.DisplayAlerts = True
End If
Next s
For Each s In ActiveWorkbook.Sheets
If s.Name Like "*Sheet 2*" Then
Application.DisplayAlerts = False
s.Delete
Application.DisplayAlerts = True
End If
Next s
End Sub
也尝试过:
Sub Export()
Dim FlSv As Variant
Dim MyFile As String
Dim MyTemplate As String
Dim sh As Worksheet
Dim wbNew As Workbook
With Application
.ScreenUpdating = False
.DisplayAlerts = False
End With
'Set sh = ActiveWorkbook.Sheets("Overrun")
'sh.Copy
'Set wbNew = ActiveWorkbook
ActiveWorkbook.workheets(Array("Sheet 1", "Sheet 2")).Copy
'there is a new activeworkbook with two worksheets
Set wbNew = ActiveWorkbook
MyFile = Replace("Consolidated", ".xlsm", "")
FlSv = Application.GetSaveAsFilename(MyFile, fileFilter:="Excel Files (*.xlsx), *.xlsx)", Title:="Enter your file name")
wbNew.SaveAs FlSv, FileFormat:=51
wbNew.Close
For Each s In ActiveWorkbook.Sheets
If s.Name Like "*Overrun*" Then
Application.DisplayAlerts = False
s.Delete
Application.DisplayAlerts = True
End If
Next s
End Sub
Sub all()
End Sub
答案 0 :(得分:0)
使用工作表名称数组。
dim wb as workbook
ActiveWorkbook.sheets(array("Sheet 1", "Sheet 2")).copy
'there is a new activeworkbook with two worksheets
set wb = ActiveWorkbook