VBA Save File As Previous Work Day

jtbrolly

New Member
Joined
Aug 22, 2011
Messages
42
Two questions:

I'm using the following to save a file down as today's date:
Code:
ActiveWorkbook.SaveAs Filename:="Name" & _
    Format(Now(), "mm-dd-yyyy"), FileFormat:=xlNormal, Password:="", _
    WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False

But I'm trying to figure out how to do two other things for a different file.

First, how can I save it as yesterday's date, specifically yesterday's business date so I don't have to change it on Monday's.

And secondly, how do I save it with the date in the middle of two static words.

Much obliged! ;)
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
The two static words bit would be:
Code:
ActiveWorkbook.SaveAs Filename:="File saved on " & _
    Format(Now(), "mm-dd-yyyy") &  " by A.User", FileFormat:=xlNormal, Password:="", _
    WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False

Not entirely sure what you mean by busines date, but to save as yesterday's date I think you should be able to replace the above section:
Code:
Format(Now()
with...
Code:
Format(Now()-1

...so the whole lot would look thus:
Code:
ActiveWorkbook.SaveAs Filename:="File saved on " & _
    Format(Now()-1, "mm-dd-yyyy") &  " by A.User", FileFormat:=xlNormal, Password:="", _
    WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False


NB not tested, but it should work.
 
Upvote 0
Please let us know whether or not this helped.
 
Upvote 0
Hello sykes :)

Maybe

Code:
x = Weekday(Date, vbSaturday)
Select Case x
    Case 1
        x = 2
    Case 2
        x = 3
    Case Else
    x = 1
End Select
ActiveWorkbook.SaveAs Filename:="File saved on " & _
    Format(Date - x, "mm-dd-yyyy") & " by A.User", FileFormat:=xlNormal, Password:="", _
    WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
 
Upvote 0
Hi Peter

Looks like a good solution. Hope it suits the OP.

Sykes
 
Upvote 0
I just tried it for today and it saved it as the yesterday, the 18th, which is a Sunday. I'm not too familiar with this formula so I'll investigate it furhter, but I tried changing it to:
Code:
x = Weekday(Date, vbSunday)
instead of
Code:
x = Weekday(Date, vbSaturday)

And it worked for today, so I'll see if it works tomorrow.
 
Upvote 0
*cast a bit of forum necromancy* I don't normally post to old forums but this has been a useful function at work.

I have come across a useful Excel formula that I use periodically for my reports:
Code:
=WORKDAY(TODAY(),-1)

This will give you the previous workday. If you include the option [Holidays] parameter, it will even account for that in the internal workday calculation.
Code:
WORKDAY(start_date, days, [holidays])

The benefit is that this function will wrap around the weekends and holidays to give you the previous workday datevalue (if you use negative {days}) or the next workday datevalue (if you use positive {days}).
 
Upvote 0

Forum statistics

Threads
1,224,604
Messages
6,179,857
Members
452,948
Latest member
UsmanAli786

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