1004 Error- issues with SaveAs

安稳与你 提交于 2019-12-02 12:09:34

问题


savePath = "\\local drive path"

myFileName = savePath & "Workbook Name " & saveDate

Application.DisplayAlerts = False

'MsgBox (myFileName)

wbTarget.Activate

ActiveWorkbook.SaveAs Filename:=myFileName, FileFormat:=xlOpenXMLWorkbook

When using the above code, I get an error 1004 "Method SaveAs of object_Workbook failed and can't figure out why. I'm relatively new to excel but belive I have everything set up right.

Update: Sorry should have shown the more complete code.

Sub SaveAs()


'Save As "Workbook Name"

Dim wbTarget            As Workbook
Dim wbThis              As Workbook
Dim strName             As String

Set wbTarget = Workbooks("Workbook Name")

wbTarget.Activate

Dim myFileName As String
Dim saveDate As String
Dim saveMonth As String
Dim monthNum As String
Dim savePath As String

Sheets("Raw").Select

saveDate = Range("A2").Value
saveMonth = Range("A2").Value
monthNum = Range("A2").Value


savePath = "\\local drive path"

myFileName = savePath & "Riskviews Data " & saveDate

Application.DisplayAlerts = False

'MsgBox (myFileName)

wbTarget.SaveAs Filename:=myFileName, FileFormat:=xlOpenXMLWorkbook



End Sub

I updated the last part to say "wbTarget.SaveAs" but that didn't seem to work.


回答1:


The problem may be with ActiveWorkBook. You should try to avoid relying on .Active, .Activate, and .Select when you can simply refer to your objects directly.

Delete wbTarget.Activate and try the below,

WbTarget.SaveAs Filename:=myFileName, FileFormat:=xlOpenXMLWorkbook



回答2:


I can see you have tried to debug. MsgBox myFileName (notice: no ()) should have displayed what you are passing to the save command. From your code above, it would be \\local drive pathWorkbook Name - noting that there is a space at the end as well. You have not defined saveDate, you have not added an extension and you have not indicated the file format.

See the following answers for further information to SaveAs.

How to do a "Save As" in vba code, saving my current Excel workbook with datestamp?



来源:https://stackoverflow.com/questions/51370126/1004-error-issues-with-saveas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!