VBA Save as giving error

ExcelNoob222

Board Regular
Joined
Jun 17, 2020
Messages
77
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have the below VBA code to save a file but I get a "Run-Time error '1004': Method 'SaveAs' of object '_workbook' failed.

It then highlights this row: ActiveWorkbook.SaveAs filename:=Path & filename & " Template.xlsx", FileFormat:=51

VBA Code:
 ET.Activate
    Dim Path As String
    Dim filename As String
    Path = "C:\New"
    filename = Range("C8")
    ActiveWorkbook.SaveAs filename:=Path & filename & " Template.xlsx", FileFormat:=51
    ActiveWorkbook.Close False


I cant seem to get this fixed. Any ideas?
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
What is in cell C8?

In these cases, it is often helpful to return what you are building to a MsgBox, so you can see exactly what it is you are trying to save, i.e.
Rich (BB code):
 ET.Activate
    Dim Path As String
    Dim filename As String
    Path = "C:\New"
    filename = Range("C8")
    MsgBox Path & filename & " Template.xlsx"
    ActiveWorkbook.SaveAs filename:=Path & filename & " Template.xlsx", FileFormat:=51
    ActiveWorkbook.Close False
Often times when you see it, the issue is obvious (missing directory slashes, using invalid characters in file name, like date slashes, etc).
 
Upvote 0
What is in cell C8?

In these cases, it is often helpful to return what you are building to a MsgBox, so you can see exactly what it is you are trying to save, i.e.
Rich (BB code):
 ET.Activate
    Dim Path As String
    Dim filename As String
    Path = "C:\New"
    filename = Range("C8")
    MsgBox Path & filename & " Template.xlsx"
    ActiveWorkbook.SaveAs filename:=Path & filename & " Template.xlsx", FileFormat:=51
    ActiveWorkbook.Close False
Often times when you see it, the issue is obvious (missing directory slashes, using invalid characters in file name, like date slashes, etc).

Ahhh that is a great idea. Had an "illegal" character in the cell. Thank you!
 
Upvote 0
You are welcome.
Glad we were able to help.
 
Upvote 0

Forum statistics

Threads
1,214,907
Messages
6,122,185
Members
449,071
Latest member
cdnMech

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top