Run time error 1004 - Method 'SaveAs' of object _workbook failed

dannyboy1982

Board Regular
Joined
Mar 6, 2015
Messages
60
Hi,

I have the following code that I have been using for a few days with no problem, and today I started getting 'Run time 1004' error messages.

I am creating a new workbook, combining the contents of cell B1 and Y2 to create a file name and saving it as a .xlsm file.

Code:

Code:
Sub NewWB()
Dim name1 As String
Dim name2 As String

name1 = ActiveWorkbook.Sheets(1).Range("B1").Value
name2 = ActiveWorkbook.Sheets(1).Range("Y2").Value

Application.ScreenUpdating = False
Workbooks.Add
'Saving the Workbook
ActiveWorkbook.SaveAs "C:\DR\Cp and Cpk\" & name1 & " " & name2 & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

Workbooks("SPC V1.xlsm").Sheets("Job dimensions").Activate
Call data
ActiveWorkbook.Save
ActiveWindow.Close
End Sub

Any help with this would be greatly appreciated.

Regards

DR
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Not sure this is the issue as you have used it before but I wouldn't have thought you need the " before name1

ActiveWorkbook.SaveAs "C:\DR\Cp and Cpk\" name1 & " " & name2 & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
 
Upvote 0
Hi

One of the reasons would be an invalid name

Before the saving check the value of the name:

Code:
MsgBox "C:\DR\Cp and Cpk\" & name1 & " " & name2 & ".xlsm"

What do you get (when the saving fires the error)?
 
Upvote 0
Hi, by adding in this line, I get a msgbox saying C:\DR\Cp and Cpk\ .xlsm, which appears to be the problem, vba is not pulling data from B1 and Y2 to create the file name. But nothing has changed since it was working. Are name1 and name2 defined correctly (dim as string)?
 
Upvote 0
I have removed the activesheet line where name1 and name2 were defined and added in the specific location, i.e. Workbook, sheet and cell range and now it appears to be working.

Code:
name1 = Workbooks("SPC V1.xlsm").Sheets("Job dimensions").Range("B1").Value
name2 = Workbooks("SPC V1.xlsm").Sheets("Job dimensions").Range("Y2").Value

instead of:

Code:
name1 = ActiveWorkbook.Sheets(1).Range("B1").Value name2 = ActiveWorkbook.Sheets(1).Range("Y2").Value
 
Upvote 0

Forum statistics

Threads
1,226,317
Messages
6,190,249
Members
453,603
Latest member
Mitch90

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