[英]Method 'SaveAs' of object '_Workbook' failed VBA
I have a workbook that is connected to a database and when you refresh the workbook, it refreshes your data in the workbook. 我有一个连接到数据库的工作簿,当您刷新工作簿时,它会刷新您的工作簿中的数据。 Afterwards a macro is ran to save the new (raw) data in two different locations, then the macro will do analysis on that raw data, and save the same workbook later on in 4 other locations.
之后,运行一个宏以将新(原始)数据保存在两个不同的位置,然后该宏将对该原始数据进行分析,然后稍后将同一工作簿保存在其他四个位置。 When I am trying to do the first save of the raw data, I am able to save to the first location, but when it moves to the second location I am getting the error: Run-time error '1004': Method 'SaveAs' of object '_Workbook' failed.
当我尝试第一次保存原始数据时,我能够保存到第一个位置,但是当它移到第二个位置时,我得到了错误:运行时错误'1004':方法'SaveAs'对象“ _Workbook”失败。 Both of the locations are valid locations.
这两个位置都是有效位置。
Here is one sub in the macro that does the first file save: 这是执行第一个文件保存的宏中的一个子:
Sub saveraw()
Dim rdate As String
Dim rawfilename As String, rawfilename1 As String
Dim mywb As Workbook
Set mywb = ActiveWorkbook
rdate = Format(Now(), "mm-dd-yy")
rawfilename = "\\sw\mes\PS\SC\SCM_Supply_Execution\Spares\This Weeks Number Database\This Weeks Numbers(raw) " & rdate & ".xlsm"
rawfilename1 = "\\sw\mes\PS\SC\SCM_Shared\Spares Reports\This Weeks Numbers(raw) " & rdate & ".xlsm"
mywb.Save
Worksheets("Sheet2").Range("A2") = Null
Application.DisplayAlerts = False
mywb.SaveAs Filename:=rawfilename
mywb.SaveAs Filename:=rawfilename1
Application.DisplayAlerts = True
End Sub
I have also tried using the method of 我也尝试过使用
ActiveWorkbook.SaveAs
in place of both of the mywb.SaveAs, but that did not work either. 代替两个mywb.SaveAs,但这也不起作用。
As a test, run the code against file locations only YOU have access to. 作为测试,对只有您有权访问的文件位置运行代码。 I'm noticing a reference to shared drive on the second command so maybe someone is already in it and it can't be saved over.
我注意到第二个命令上对共享驱动器的引用,因此可能有人已经在其中,并且无法保存。
Set display alerts to true for now. 现在将显示警报设置为true。
Also, When you run the test macro, make sure you don't have any windows explorer windows open and feed back here with results and I'll assist if it debugs again. 另外,在运行测试宏时,请确保没有打开任何Windows资源管理器窗口,并在此处反馈结果,如果它再次调试,我将为您提供帮助。
On the face of it, the code looks good to me. 从表面上看,代码对我来说看起来不错。
Try this 尝试这个
ActiveWorkbook.SaveAs Filename:="\\sw\mes\PS\SC\SCM_Supply_Execution\Spares\This Weeks Number Database\This Weeks Numbers(raw) " & rdate & ".xlsm" _
, FileFormat:=xlOpenXMLWorkbookMacroEnabled
This might have issues if the file already exists because then a pop-up will ask you if you want to overwrite it or not 如果该文件已经存在,则可能会出现问题,因为随后会弹出一个对话框,询问您是否要覆盖它
Though I think the error is due to an issue with privileges in that specific location or connection issues 尽管我认为该错误是由于该特定位置的特权问题或连接问题引起的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.