VBA Dynamic filename

purpleozzie

Board Regular
Joined
Jun 8, 2015
Messages
64
Hi guys,

I use this piece of VBA Code to save a file as a PDF on to a users desktop.. I would like to change it so it changes the filename depnding on a cell reference and todays date...

Sub create()
'
' Macro1 Macro
'

'
Sheets("QUOTE SHEET").Select
Sheets("QUOTE SHEET").Copy
Dim oSHELL As Object, sDesktopPath As String
Set oSHELL = CreateObject("WScript.Shell")
sDesktopPath = oSHELL.SpecialFolders("Desktop")


ChDir sDesktopPath
ActiveWorkbook.SaveAs Filename:= _
sDesktopPath & "\Customercopy.xlsx", FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
sDesktopPath & "\Customercopy.pdf", Quality:=xlQualityStandard _
, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
True
ActiveWindow.Close
End Sub

the cell reference is from a different worksheet called 'June 2015' and I want the date format in dd-mm-yy..

Next month the worksheet will be renamed July 2015 and I also want it to be able to update to still work..

Cheers,
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Change the filename part (till comma) to:

ActiveWorkbook.SaveAs Filename:= _
sDesktopPath & Activeworkbook.sheets("June 2015").cells(3,4).value & TEXT(NOW(),"mmm-yyyy"),

Need to change Cells(3,4) to the cell that contains the file name.
 
Upvote 0
To dynamically identify the sheet name every month (without having to hard code it), you can use this block of code to dynamically build your file name:
Code:
    Dim mySheetName As String
    Dim myFileName As String
'   Figure out sheet name from current date
    mySheetName = Format(Date, "mmmm yyyy")
'   Build file name by looking up cell A1 on sheet name and add date
    myFileName = Sheets(mySheetName).Range("A1") & Format(Date, "dd-mm-yy")
You didn't say which cell you want to pull the name from, so I assumed "A1". Just replace "A1" with whatever cell you want to return it from.

So the just use "myFileName in place of your hard-coded file name, and add the extension.
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,717
Members
448,985
Latest member
chocbudda

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