VBA to rename file to last sunday to saturday

Knockoutpie

Board Regular
Joined
Sep 10, 2018
Messages
116
Office Version
  1. 365
Platform
  1. Windows
Hi everyone, i've had luck getting VBA to rename a file based on the current date.
However, every Monday I work on a file that's dated from Last Sunday to Last Saturday.

When I rename this file I name it "POS 20190120-20190126"

YY-MM-DD

Can anyone help me have VBA automatically name/save the file to last weeks Sunday-Saturday? I'd appreciate any tips. :confused:
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Code:
Sub t()
Dim fName As String
fName = "POS " & Format(Date - 8, "yyyymmdd") & "-" & Format(Date - 2, "yyyymmdd")
MsgBox fName
End Sub
 
Last edited:
Upvote 0
Code:
Sub t()
Dim fName As String
fName = "POS " & Format(Date - 8, "yyyymmdd") & "-" & Format(Date - 2, "yyyymmdd")
MsgBox fName
End Sub


Thanks! Works great, i used a little help from Chip also!

https://answers.microsoft.com/en-us...e-a-file/e85bea4e-1e82-e011-9b4b-68b599b31bf5

Code:
Sub (macro)
'


'


Dim fName As String
Dim FPath As String
fName = "POS " & Format(Date - 8, "yyyymmdd") & "-" & Format(Date - 2, "yyyymmdd")
MsgBox fName


FPath = "C:\Users\paul.ferguson\Downloads"


ThisWorkbook.SaveAs Filename:=FPath & "" & fName & ".xlsm", _
    FileFormat:=xlOpenXMLWorkbookMacroEnabled
'
End Sub
 
Upvote 0
Code:
[COLOR=#333333]Sub (macro)[/COLOR]'


'


Dim fName As String
Dim FPath As String
fName = "POS " & Format(Date - 8, "yyyymmdd") & "-" & Format(Date - 2, "yyyymmdd")
MsgBox fName


FPath = "C:\Users\paul.ferguson\Downloads"


ActiveWorkbook.SaveAs Filename:=FPath & "" & fName & ".xlsm", _
    FileFormat:=xlOpenXMLWorkbookMacroEnabled
' [COLOR=#333333]End Sub[/COLOR]

I had to change to ActiveWorkbook to open files.

Thanks for your help!
 
Upvote 0
Currently what I have works great, and I can continue to use it. However on the occasion that I take a Monday off and return tuesday, the dates are off because the report was not run on Monday. Any way to fix this?

VBA Code:
Dim fName As String
Dim FPath As String
fName = "POS " & Format(Date - 8, "yyyymmdd") & "-" & Format(Date - 2, "yyyymmdd")
MsgBox fName
 
Upvote 0
You can try this and see if does what you want.

Code:
fName = "POS " & Format(Date - (7 + Weekday(Date, vbSunday)), "yyyymmdd") & "-" & Format(Date - 2, "yyyymmdd")
 
Upvote 0

Forum statistics

Threads
1,223,895
Messages
6,175,257
Members
452,625
Latest member
saadat28

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