此代码效果很好,只是保存了工作簿中的每个工作表。我需要它将工作簿中的每个工作表保存为单独的文件。我需要它单独保存每个选项卡,为新文件提供与工作表相同的名称。我知道这将涉及复制或移动每个选项卡然后保存,但我不知道该怎么做。可以编辑我当前的代码来完成我想要的吗?
Sub Save_Tabs()
Dim ws As Excel.Worksheet
Dim SaveToDirectory As String
Dim filename As String
Dim CurrentWorkbook As String
Dim CurrentFormat As Long
CurrentWorkbook = ThisWorkbook.FullName
CurrentFormat = ThisWorkbook.FileFormat
SaveToDirectory = "E:\NYCHHC\CSOD\Reports\completion reports\"
For Each ws In Application.ActiveWorkbook.Worksheets
Application.DisplayAlerts = False
ws.SaveAs SaveToDirectory & ws.Name ' FileFormat:=xlNormal, CreateBackup:=False
Next
End Sub
感谢您的帮助
Copy
worksheet对象的方法将创建一个新的工作簿。微软文档:
For Each ws In Application.ActiveWorkbook.Worksheets
ws.Copy
ActiveWorkbook.SaveAs SaveToDirectory & ws.Name & ".xlsx", FileFormat:=xlNormal, CreateBackup:=False
ActiveWorkbook.Close False
Next