Macro to save sheet as value in cell

BMcHale

New Member
Joined
Sep 26, 2017
Messages
18
Hi all,

I'm trying to write a macro that saves a sheet as a unique name, varying upon the value of a specific field.

For example a name I would be after would be "JR Export MAR".

This is what I have so far, however it is't working.

Code:
Sub Macro1()
'
' Macro1 Macro
'


'
    Dim Filename1 As String
    Filename1 = Workbooks("JR CSV Creator.xlsm").Sheets("Input").Range("A1").Value
    
    Workbooks.Add
    ChDir "M:\Palanning\Transport\Trucks"
    ActiveWorkbook.SaveAs Filename:= _
        "M:\Palanning\Transport\Trucks\JR Export & Filename1 & .xlsx", FileFormat:=xlsx _
        , CreateBackup:=False

End Sub

Any Ideas?

Thanks,
Billy
 
Last edited:

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Hi,
please use CODE tags when pasting code.

Code:
[COLOR=#333333]Sub SaveCustomName()[/COLOR]

[COLOR=#333333]Dim Filename1 As String[/COLOR]
[COLOR=#333333]Filename1 = Workbooks("JR CSV Creator.xlsm").Sheets("Input").Range("A1").Value[/COLOR]

[COLOR=#333333]Workbooks.Add[/COLOR]
[COLOR=#333333]ChDir = "M:\Palanning\Transport\Trucks\"[/COLOR]
[COLOR=#333333]ActiveWorkbook.SaveAs Filename:= ChDir & "[/COLOR][COLOR=#333333]JR Export " & Filename1 & ".xlsx", FileFormat:=xlsx _[/COLOR]
[COLOR=#333333], CreateBackup:=False[/COLOR]

[COLOR=#333333]End Sub[/COLOR]

Does this work for you?
 
Upvote 0
Hi,
Yes sorry about that, only realised I hadn't once I'd posted.

There's an error being thrown up highlighting ChDir saying "Argument not optional"

Thanks,
Billy
 
Upvote 0
I wasn't familiar with ChDir, thought it was just a regular variable. Change ChDir to sPath and you'll be fine
 
Upvote 0
try
Code:
Sub Macro1()
'
' Macro1 Macro
'


'
    Dim Filename1 As String
    Filename1 = Workbooks("JR CSV Creator.xlsm").Sheets("Input").Range("A1").Value
    
    Workbooks.Add
    ChDrive "M:"
    ChDir "M:\Palanning\Transport\Trucks"
    ActiveWorkbook.SaveAs fileName:= _
        "M:\Palanning\Transport\Trucks\JR Export " & Filename1 & ".xlsx", FileFormat:=51
End Sub
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,386
Messages
6,119,215
Members
448,874
Latest member
b1step2far

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