SaveAs Method with Filename Derived From cell reference

wmcnabb084

New Member
Joined
Sep 23, 2010
Messages
9
I am trying to use a cell reference to have excel automatically Save a worksheet using a cell reference as the workbook name. When I step through the code and use the immediate window I get the correct name for the file (fname in this case) but as soon as Excel tries to save the file, I get an error.

Here is the code I am using:

fname = Sheet3.Range("I1").Value
ChDir "S:\Op Costs\Budget 2013\Data"
MsgBox "The actice file will be saved as " & fname
ActiveWorkbook.SaveAs filename:= _
"S:\Op Costs\Budget 2013\Data\" & fname & ".xlsm", FileFormat:= _
xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

Any help would be most appreciated...
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Change this line:
fname = Sheet3.Range("I1").Value

To:
fname = Sheets("Sheet3").Range("I1").Value

Including Option Explicit in your code might help catch this in the future.



Also you misspelled "active" in your message box. ;)
 
Last edited:
Upvote 0
try doing:

Code:
Dim wb As Workbook
Set wb = ActiveWorkbook

fname = wb.Sheets("Sheet3").Range("I1").Value
     ChDir "S:\Op Costs\Budget 2013\Data"
     MsgBox "The actice file will be saved as " & fname
     wb.SaveAs filename:= _
         "S:\Op Costs\Budget 2013\Data\" & fname & ".xlsm", FileFormat:= _
         xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

Maybe it has something to do with how your ChDir is changing directories. Never used that before. Why do that? (wondering)
 
Upvote 0
Still getting same SAveAs Method error.

Current code module:

Sub TestSaveAsOPEX()
'
'
Dim fname As Variant
Dim wb As Workbook
Set wb = ActiveWorkbook
fname = wb.Sheets("Input Sheet").Range("I1").Value
ChDir "S:\Op Costs\Budget 2013\Data"
MsgBox "The active file will be saved as " & fname
wb.SaveAs filename:= _
"S:\Op Costs\Budget 2013\Data\" & fname & ".xlsm", FileFormat:= _
xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

End Sub

I have r/w access to path mentioned and can run this code fine if I hard code a file name such as

Sub TestSaveAsOPEX()
'
'
Dim fname As Variant
Dim wb As Workbook
Set wb = ActiveWorkbook
' fname = wb.Sheets("Input Sheet").Range("I1").Value
ChDir "S:\Op Costs\Budget 2013\Data"
MsgBox "The active file will be saved as " & fname
wb.SaveAs filename:= _
"S:\Op Costs\Budget 2013\Data\testFile.xlsm", FileFormat:= _
xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

End Sub
 
Upvote 0
Aghhhhhhhhhhhhhhhhh

The name in Sheet3 I1 had an '/' character in the name so I will have to check that first and substitute the '/' character with a '_'

Thank you so much for your effort and help!

WM
 
Upvote 0

Forum statistics

Threads
1,215,235
Messages
6,123,792
Members
449,126
Latest member
Greeshma Ravi

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