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.
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
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?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.