Error 1004 save and close excel document

cstuder

New Member
Joined
May 15, 2023
Messages
20
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
 
Well, it worked but when I open up the (saved) document it's blank. It didn't save the file I had open. Do I have something wrong? This is a shared drive. Would that have anything to do with it? Thanks for sticking with me.

1692391372208.png
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Is the workbook you are trying to save the same one where this VBA code resides?
Or do you have two workbooks open?
Are you trying to do this on a Desktop/Laptop computer or using Excel online?
 
Upvote 0
All of my macros are saved as "personal" and in a hidden workbook. So technically yes?
I only had one workbook open. I then closed excel to ensure nothing was open with the same result.
desktop
 
Upvote 0
All of my macros are saved as "personal" and in a hidden workbook. So technically yes?
I only had one workbook open. I then closed excel to ensure nothing was open with the same result.
desktop
That there is your problem then.
"ThisWorkbook" refers to the workbook this VBA procedure is found in. You do NOT want to save that one.
You want to save the other workbook with your data. That would be "ActiveWorkbook".

Try this (I am assuming that there is no VBA in your data workbook that you want to save):
VBA 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 & ".xlsx"

ActiveWorkbook.SaveAs rootDir & FileName

ActiveWorkbook.Close

End Sub
 
Upvote 0
Solution
That did it! Made my day! I'll keep the Active Workbook and This Workbook in mind when trying to create macros. Thank you so much!
 
Upvote 0
You are welcome.

Yes, the thing to remember is:
ThisWorkbook = the workbook that the VBA module that is running is located in
ActiveWorkbook = the workbook that is active when the VBA code is running

Many times, these are "one and the same", i.e., when the VBA code exists in the workbook you are running it against.
But it is not in your case, since the VBA code is in your Personal Macros workbook.
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,988
Members
452,373
Latest member
TimReeks

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