是否可以使用 VBA 选择 Excel 工作簿中的某些工作表并保存为新的工作簿文件?
答案1
是的,这是一个样本。
Sub Sheet_SaveAs()
Dim wb As Workbook
Sheets("Sheet1").Copy
Set wb = ActiveWorkbook
With wb
.SaveAs ThisWorkbook.Path & "\Workbookname.xlsx"
'.Close False
End With
End Sub
答案2
谢谢 Petr。我设法通过录制自己并谷歌搜索其余内容来拼凑下面的代码,因为我需要从其中一个工作簿中保存多个工作簿并自动添加日期。它似乎有效。
Sub Seperate_Sheets()
Dim Path1 As String
Dim Path2 As String
Dim Path3 As String
Path1 = ActiveWorkbook.Path & "\" & "Tracker 1" & Format(Now, " dd-mm-yyyy ")
Path2 = ActiveWorkbook.Path & "\" & "Tracker 2" & Format(Now, " dd-mm-yyyy ")
Path3 = ActiveWorkbook.Path & "\" & "Tracker 3" & Format(Now, " dd-mm-yyyy ")
Sheets(Array("sheet1", "sheet2", "sheet3")).Move
ActiveWorkbook.SaveAs Filename:=Path1, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
Sheets(Array("sheet3", "sheet4", "sheet5")).Move
ActiveWorkbook.SaveAs Filename:=Path2, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
Sheets(Array("sheet6", "sheet7", "sheet8")).Move
ActiveWorkbook.SaveAs Filename:=Path3, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
End Sub