VBA Saving File with Week Number

Littlemalky

Board Regular
Joined
Jan 14, 2011
Messages
223
I need to Save As with this file name exactly:

"BO Priority April Week 3.xlsx"

The month part is easy, i just declare it as Format(Date, "mmmm"); however, the "Week 3" part is tricky because it is number by the amount of week's in a month, not the week it would be in a year (which seems to be the only functions I could find).

Does anyone know how to declare this so that when I save each week during the month, it will be accurate?
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Yeah; use one variable for each month (a01-a12), assign the number of days to each variable (a01 = 31, If Not LeapYear = True Then a02 = 28, etc). A third variable should equal Day(Date), and you can then use Month(Date) to figure out which "aXX" variable's scopre your current days falls under. Next you want to add all the values of each aXX that falls before the one you're now working with. Last, you want to subtract the value of your current Day(Date) from your contemporaneous aXX.
 
Upvote 0
Workbooks.SaveAs (yourPath & SailWeek(yourDate As Date) & ".xls")

This would create the filename "Week X.xls" at yourPath.
 
Upvote 0
THAT WORKED!!!

Now here's another problem i just realized. This function is awesome; however, I need the first Monday of the month to be the first week. For example, April 1st is on a Friday in this month. However, every Friday for the following week, and we consider that Monday through friday Week 1, so it would be the 4th through the 8th. This **** is getting technical.
 
Upvote 0
So this is what I have thus far, but I just need to adjust it in some way to factor in the first Monday of the month as Week 1:
Code:
Private Sub SaveAs()
    Workbooks.Open Filename:="C:\...\BO Priority.xlsx"
    ActiveWorkbook.SaveAs "C:...\BO Priority " & Format(Date, "mmmm") & " " & SailWeek(Date) & ".xlsx"
End Sub
Function SailWeek(dDate1 As Date) As String
    Dim dDate2 As String
    Dim wWeek As Integer
     
     'dDate2 is changed to date from String
     
    dDate2 = VBA.CDate(Month(dDate1) & "/01/" & Year(dDate1))
    wWeek = DateDiff("ww", dDate2, dDate1, vbMonday, vbUseSystem) + 1
     
     'Return the Week number of the month
     'Monday is taken as week starting date, you can change
     'it to your desired day as starting date of week
     
    SailWeek = "Week " & wWeek
End Function
 
Upvote 0
So this is what I have thus far, but I just need to adjust it in some way to factor in the first Monday of the month as Week 1:
Code:
Private Sub SaveAs()
    Workbooks.Open Filename:="C:\...\BO Priority.xlsx"
    ActiveWorkbook.SaveAs "C:...\BO Priority " & Format(Date, "mmmm") & " " & SailWeek(Date) & ".xlsx"
End Sub
Function SailWeek(dDate1 As Date) As String
    Dim dDate2 As String
    Dim wWeek As Integer
     
     'dDate2 is changed to date from String
     
    dDate2 = VBA.CDate(Month(dDate1) & "/01/" & Year(dDate1))
    wWeek = DateDiff("ww", dDate2, dDate1, vbMonday, vbUseSystem) + 1
     
     'Return the Week number of the month
     'Monday is taken as week starting date, you can change
     'it to your desired day as starting date of week
     
    SailWeek = "Week " & wWeek
End Function
The function appears to assign week 1 to the week in which the first day of the month falls. Do you want week 1 to be the week in which the first Monday of the month occurs?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,135
Members
452,890
Latest member
Nikhil Ramesh

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