对象'_Workbook'的方法'SaveAs'失败

时间:2019-02-11 20:29:39

标签: excel vba save-as

我在其他2个文件中的以下代码运行正常,但是由于某种原因,我在运行时遇到了ojbect的SaveAs错误。甚至更奇怪的是,如果我调试并再次点击运行,它将完成而没有错误。

Sub saveWorksheet()

Set report = ActiveWorkbook.Worksheets("Report")
sb_Copy_Save_Worksheet_As_Workbook

End Sub


Sub sb_Copy_Save_Worksheet_As_Workbook()
Dim wb As Workbook
Set wb = Workbooks.Add
ThisWorkbook.Sheets("Report").Copy Before:=wb.Sheets(1)
wb.Sheets(1).Name = Day(Now) & "-" & MonthName(Month(Now), True) & "-" & 
Year(Now) & " " & getTime
Application.DisplayAlerts = False
wb.Sheets("Sheet1").Delete

wb.SaveAs "\\Naeast.ad.jpmorganchase.com\amerawm$\AM\NAAMSHARE13\AM 
Oversight and control\DE Program Team\User Tools\14. Daily 
Trackers\Compliance Check Archive\" & Month(Date) & "-" & Day(Date) & "-" 
& Year(Date) & "-" & "New_Compliance_Check.xlsx"
wb.Close
Application.DisplayAlerts = True

End Sub

Function getTime()

If Len(Time) = 10 Then
    getTime = Left(Time, 1) & "." & Mid(Time, 3, 2)
Else
    getTime = Left(Time, 2) & "." & Mid(Time, 4, 2)
End If

End Function

突出显示的调试行是:

wb.SaveAs "\\Naeast.ad.jpmorganchase.com\amerawm$\AM\NAAMSHARE13\AM Oversight and control\DE Program Team\User Tools\14. Daily Trackers\Compliance Check Archive\" & Month(Date) & "-" & Day(Date) & "-" & Year(Date) & "-" & "New_Compliance_Check.xlsx"

编辑:我根据您的建议将代码更新为此代码,但还是相同的问题。

wb.Sheets("Sheet1").Delete
wb.Activate
wb.SaveAs "\\Naeast.ad.jpmorganchase.com\amerawm$\AM\NAAMSHARE13\AM 
Oversight and control\DE Program Team\User Tools\14. Daily 
Trackers\Compliance Check Archive\" & Month(Date) & "-" & Day(Date) & "-" & 
Year(Date) & "-" & "New_Compliance_Check.xlsx"
wb.Close

1 个答案:

答案 0 :(得分:0)

在遇到Excel似乎对我指的是哪个工作簿感到困惑之前,我曾遇到过这样的情况。在wb.Activate之前尝试wb.SaveAs

也(不确定是否适用于您),但是当保存到Sharepoint时,我不得不有时将文件路径中的所有\反转为/