Adding Today's Date to a Workbook Using VBA

Luis_B

New Member
Joined
Oct 13, 2021
Messages
38
Office Version
  1. 365
Platform
  1. Windows
Hi Guys,

I need help adding a date to a Workbook that I create using VBA. Below is the code that I'm using to create the workbook and to save it to CSV. It there a way to add a formula to this code to add the date to the actual name of the file? so the name of the file would look something like this "WK1-401K081022"

'Create a new file that will receive the data
Set newWB = Workbooks.Add
With newWB
Set newS = newWB.Sheets("Sheet1")
newS.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
newWB.Sheets("Sheet1").Range("B:B").NumberFormat = "m/d/yyyy"

'Save in CSV
Application.DisplayAlerts = False
.SaveAs Filename:="C:\DOWNLOADS\WK1-401K.csv", FileFormat:=xlCSV
Application.DisplayAlerts = True
Workbooks("WK1-401K").Close SaveChanges:=True 'SaveAndCloseExcelSheet

Thank you, I appreciate any help
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
tRY
VBA Code:
"C:\DOWNLOADS\WK1-401K" & FORMAT(NOW,"MMDDYY") & ".csv"
Thank you Dave, that worked great. Would you be able to show me how to close the workbook after it is created with the date in the name? I tried the code below but it doesn't work

Workbooks("WK1-401K"& Format(Now, "MMDDYY").Close SaveChanges:=True 'SaveAndCloseExcelSheet

The entire code looks like this after I applied your adjustment

Application.DisplayAlerts = False
.SaveAs Filename:="C:\DOWNLOADS\WK1-401K" & Format(Now, "MMDDYY") & ".csv"
Application.DisplayAlerts = True
Workbooks("WK1-401K" & Format(Now, "MMDDYY").Close SaveChanges:=True 'SaveAndCloseExcelSheet

Thank you,
 
Upvote 0
I thinking, since you already saved newWB, all you need is .close to close the workbook, before your end with, no need for the workbook().close save stuff.
 
Upvote 0
I thinking, since you already saved newWB, all you need is .close to close the workbook, before your end with, no need for the workbook().close save stufHello Dave,

Hi Dave,

I have been getting this message below when I open the files. Do you happened to know how to fix this issue. I would need to save the file to CSV

Thank you,


1661860825664.png



Set newWB = Workbooks.Add
With newWB
Set newS = newWB.Sheets("Sheet1")
newS.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
newWB.Sheets("Sheet1").Range("B:B").NumberFormat = "m/d/yyyy"

'Save in CSV
Application.DisplayAlerts = False
.SaveAs Filename:="C:\DOWNLOADS\WK1-401K" & Format(Now, "MMDDYY") & ".csv"
Application.DisplayAlerts = True
Workbooks("WK1-401K" & Format(Now, "MMDDYY")).Close SaveChanges:=True 'SaveAndCloseExcelSheet


End With
 
Upvote 0
When saving a sheet as csv, you would need to tell it the csv Fileformat.

If you use the macro recorder, you will see what I mean.

For example:

VBA Code:
    ActiveWorkbook.SaveAs Filename:="C:\Users\dmorrison\Downloads\MyBook.csv" _
        , FileFormat:=xlCSV, CreateBackup:=False
 
Upvote 0
When saving a sheet as csv, you would need to tell it the csv Fileformat.

If you use the macro recorder, you will see what I mean.

For example:

VBA Code:
    ActiveWorkbook.SaveAs Filename:="C:\Users\dmorrison\Downloads\MyBook.csv" _
        , FileFormat:=xlCSV, CreateBackup:=False
I forgot about using the macro recorder. Thank you again for your help. That fixed my problem. Have an awesome day
 
Upvote 0

Forum statistics

Threads
1,215,839
Messages
6,127,191
Members
449,368
Latest member
JayHo

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