我正在尝试将特定工作表从我的工作簿保存到另一个工作簿,并尝试将其保存在我当前工作簿的路径中。保存选项的方式应该是使用dd.mm.yyyy保存。
我尝试了以下代码,我在行
中收到应用程序定义错误> newWB.SaveAs filename:=Path2 & Format(Now(), "yyyymmdd") &
> myWorksheets(i), FileFormat:=xlsx
你能帮我弄清楚我错在哪里吗?
Sub save()
Dim myWorksheets() As String 'Array to hold worksheet names to copy
Dim newWB As Workbook
Dim CurrWB As Workbook
Dim i As Integer
Dim path1, Path2
path1 = ThisWorkbook.Path
Path2 = path1 & "\TT"
Set CurrWB = ThisWorkbook
myWorksheets = Split("Report", ",")
For i = LBound(myWorksheets) To UBound(myWorksheets) 'Go through entire array
Set newWB = Workbooks.Add 'Create new workbook
CurrWB.Sheets(Trim(myWorksheets(i))).Copy
newWB.SaveAs filename:=Path2 & Format(Now(), "yyyymmdd") & myWorksheets(i), FileFormat:=xlsx
newWB.Close saveChanges:=False
Next i
CurrWB.Save 'save original workbook.
End Sub
答案 0 :(得分:0)
@Jenny - 此代码将完成您在问题中提出的问题,它会将ActiveSheet保存为新文件;并且可以用作可以在代码中调用的函数。你可以改变" Rpt"确定新工作簿的标题。当我写vba代码时,我总是试着跟随母亲用来对我说的话,"保持简单"。
Application.DisplayAlerts = False
Dim wbPath As String
wbPath = ThisWorkbook.Path
ActiveSheet.Copy
ActiveWorkbook.SaveAs Filename:=wbPath & "\" & "Rpt" & " " & Format(Date, "yyyymmdd") & ".xlsx", _
FileFormat:=51, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
Application.DisplayAlerts = True