VBA code to add cell information to file name

STIRRELL

Board Regular
Joined
Dec 30, 2010
Messages
63
Office Version
  1. 365
Hi all,
I am working on a macro to save off a file. We would like to pull a few cells of data from a spreadsheet into the name of the spreadsheet. The start date is in cell B3 on the INPUT tab. How can we pull this into the file name using VBA? I think the dashes will cause a problem so we can use underscore instead of dashes .

For example
Start Date: 02/11/22 ( or 2_11_22)

existing code:
Dim NTCT_EXPENSE_PAYROLL As Workbook
Dim CSV_FILE As Worksheet

Set shtToExport = ThisWorkbook.Worksheets("CSV_FILE") 'Sheet to export as CSV
Set wbkExport = Application.Workbooks.Add
shtToExport.Copy Before:=wbkExport.Worksheets(wbkExport.Worksheets.Count)
Application.DisplayAlerts = False 'Possibly overwrite without asking
wbkExport.SaveAs Filename:="C:\EXPORT\7J1", FileFormat:=xlCSV
Application.DisplayAlerts = True
wbkExport.Close SaveChanges:=False

thank you for your help
Sharon
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi,​
you can use the Replace VBA function on the B3 cell Text property in order to replace "/" with "-" for example …​
 
Upvote 0
Does this give you what you want?

VBA Code:
Sub Save_With_Date()
    Dim StartDate As Date
    StartDate = Worksheets("INPUT").Range("B3").Value
    
    ThisWorkbook.Sheets("CSV_File").Copy
    ActiveWorkbook.SaveAs "C:\EXPORT\7J1 " & Format(StartDate, "d_m_yy") & ".csv", FileFormat:=6
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,248
Members
452,623
Latest member
cliftonhandyman

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