Add day to todays date

Nighabi

New Member
Joined
Feb 13, 2008
Messages
34
I'm using some VB to take and excel file and save it as a csv. Part of the logic put the date in the file name
DateTime = Format(CStr(Now), "yyyymmddhhmmss")
SaveCSV = "fq_DeliveredPrice_Petrocard_haz_" & DateTime & ".csv"
fname = ThisWorkbook.Path & "\" & SaveCSV

It was working great but now they want to run it a day earlier, which means I need to name the file with tomorrows date.
I messed around and think I have something that works. Or should I say, it works now but I am worried that at the end of the month it won't roll over the month.
Can anyone tell me if this code will make the date 20110801 when they save it on 20110731 and not make it 20110732?

Today = Format(CStr(Now), "yyyymmdd") + 1
Time = Format(CStr(Now), "hhmmss")

SaveCSV = "fq_DeliveredPrice_Petrocard_haz_" & Today & Time & ".csv"
fname = ThisWorkbook.Path & "\" & SaveCSV
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
You need to add 1 to Now in the Format function. Also, the CStr() function is not needed as the Format function converts the value to string of the provide format. Try this

DateTime = Format(Now + 1, "yyyymmddhhmmss")
 
Upvote 0
To answer this question
Can anyone tell me if this code will make the date 20110801 when they save it on 20110731 and not make it 20110732?

Your code would have returned 20110732 because VB will no longer interpret this Format(CStr(Now), "yyyymmdd") as a date rather it will just see a string and try to infer its value when adding 1 and in this case it will assume it is an integer. To test this
msgbox Format(DateValue("7/31/2011"), "yyyymmdd") + 1
Returns 20110732​
 
Upvote 0

Forum statistics

Threads
1,224,526
Messages
6,179,322
Members
452,906
Latest member
Belthazar

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