Error 1004 save and close excel document

cstuder

New Member
Joined
May 15, 2023
Messages
15
Office Version
  1. 2021
Platform
  1. Windows
I receive emails containing excel documents and they open as (excel) read only. I run two other macro's to format it, then I want to save and close it (by using the below code).
I get a 1004 error code on line ThisWorkbook.SaveAs rootDir & FileName & ".xlsx" Any suggestions on how to change the code so it saves properly?


Sub CHProIII()
'
' CHProIII Macro
' save as and close file
'
Dim rootDir As String
Dim FileName As String

rootDir = "G:\REBATES\CutlerHammer\CH Pro's\"
rootDir = Replace(rootDir, "@1", Application.UserName)

FileName = ActiveSheet.Range("J3").Value & " " & ActiveSheet.Range("J1").Value & ".xlsx"

ThisWorkbook.SaveAs rootDir & FileName & ".xlsx"

ThisWorkbook.Close

End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Put this line of code before your "SaveAs" line, and tell us exactly what it returns:
VBA Code:
MsgBox rootDir & FileName & ".xlsx"
 
Upvote 0
Screenshots

1692387676271.png

1692387695694.png

1692387716729.png
 
Upvote 0
OK, first thing, is that you have the extension build into the file name already, so you don't need to add it a second time in your SaveAs statement.

But the reason you are getting an error is because you are trying to save THIS workbook, which contains VBA code, with an "xlsx" extension.
That is not allowed. You cannot save files with VBA code to an "xlsx" (macro free) extension.
You must use an extension which allows VBA, like "xlsm" or "xlsb", to name a few.
And you must save it with the appropriate corresponding FileFormat.

See: XlFileFormat enumeration (Excel)
 
Upvote 0
I did read about that on another thread, but .xlsm didn't help. How would you write that?
 
Upvote 0
If you click on the link under the File Format field in the the document I referenced, you will see that "xlsm" has value 52.
So your code should look something like:

Rich (BB code):
Sub CHProIII()
'
' CHProIII Macro
' save as and close file
'
Dim rootDir As String
Dim FileName As String

rootDir = "G:\REBATES\CutlerHammer\CH Pro's\"
rootDir = Replace(rootDir, "@1", Application.UserName)

FileName = ActiveSheet.Range("J3").Value & " " & ActiveSheet.Range("J1").Value & ".xlsm"

ThisWorkbook.SaveAs rootDir & FileName, 52

ThisWorkbook.Close

End Sub
 
Upvote 0
Double-check the screen print you posted in post 3, and confirm that the file path shown there is accurate.
 
Upvote 0
Great point....here is the directory.
1692390228142.png



Sub CHProIII()
'
' CHProIII Macro
' save as and close file
'
Dim rootDir As String
Dim FileName As String

rootDir = "G:\REBATES\CutlerHammer\CH Pro's\"
rootDir = Replace(rootDir, "@1", Application.UserName)

FileName = ActiveSheet.Range("J3").Value & " " & ActiveSheet.Range("J1").Value & ".xlsm"

ThisWorkbook.SaveAs rootDir & FileName, 52
ThisWorkbook.Close

End Sub
 
Upvote 0
Try to see if you can save it when you hard-code it, i.e.
VBA Code:
ThisWorkbook.SaveAs "G:\REBATES\CutlerHammer\CH Pro's\DRB64x6ll.xlsm", 52

If that works, then it probably means that you have some illegal character in J1 or J3 causing issues.
If that does not work, please tell me the exact error code/message you are getting.
Is it still the same message as before, or is it different?
 
Upvote 0

Forum statistics

Threads
1,215,081
Messages
6,123,016
Members
449,093
Latest member
ikke

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