Open worksheet based on this week's Monday date

bujubenji

New Member
Joined
Feb 26, 2023
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I've put together a management information workbook which has a master sheet called 'Performance.' All of the remaining sheets are titled as the Monday of each week with a format ('dd.mm.yy'), i.e. today is 17.05.23, so this week's sheet is called '15.05.23.'

I'm looking for code for a workbook open event, which will activate this week's sheet to save the hassle of selecting it from a drop down or by any other means.

Does anybody have a viable solution please? I've searched all of the forums I can find and haven't had any luck.

Thanks in advance,

Buju
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Does this work for you?

You have a period, '.', after the 15.05.23, did you mean this to be there?

VBA Code:
Private Sub Workbook_Open()
Dim Ws As Worksheet
    
    For Each Ws In ActiveWorkbook.Worksheets
        If Ws.Name = Format(DateAdd("d", 1 - Weekday(Date, vbMonday), Date), "dd.mm.yy") & "." Then
            Ws.Activate
            Exit For
        End If
    Next Ws

End Sub
 
Upvote 0
Solution
Does this work for you?

You have a period, '.', after the 15.05.23, did you mean this to be there?

VBA Code:
Private Sub Workbook_Open()
Dim Ws As Worksheet
   
    For Each Ws In ActiveWorkbook.Worksheets
        If Ws.Name = Format(DateAdd("d", 1 - Weekday(Date, vbMonday), Date), "dd.mm.yy") & "." Then
            Ws.Activate
            Exit For
        End If
    Next Ws

End Sub
Good morning Herakles,

Thanks so much for the above. It doesn't seem to work in this specific workbook- no error message at all, it just doesn't bring me to another worksheet.

I've just created a test document to trial it and it works just fine there. I suppose it's now just a case of tracking down which area of code might be interfering with this in my workbook?

Thanks so much for the assistance!
 
Upvote 0
Does tha Workbook_Open event code actually run?

Do you already have a Workbook_Open procedure with other code in it?

Try this, it will give you a message if the sheet it is looking for is not there.

VBA Code:
Private Sub Workbook_Open()
Dim Ws As Worksheet
    
    For Each Ws In ActiveWorkbook.Worksheets
        If Ws.Name = Format(DateAdd("d", 1 - Weekday(Date, vbMonday), Date), "dd.mm.yy") & "." Then
            Ws.Activate
            Exit Sub
        End If
    Next Ws

    MsgBox Format(DateAdd("d", 1 - Weekday(Date, vbMonday), Date), "dd.mm.yy") & "." & " worksheet not found" & _
        " in this workbook.", vbInformation, "Warning"

End Sub
 
Upvote 0
Does tha Workbook_Open event code actually run?

Do you already have a Workbook_Open procedure with other code in it?

Try this, it will give you a message if the sheet it is looking for is not there.

VBA Code:
Private Sub Workbook_Open()
Dim Ws As Worksheet
   
    For Each Ws In ActiveWorkbook.Worksheets
        If Ws.Name = Format(DateAdd("d", 1 - Weekday(Date, vbMonday), Date), "dd.mm.yy") & "." Then
            Ws.Activate
            Exit Sub
        End If
    Next Ws

    MsgBox Format(DateAdd("d", 1 - Weekday(Date, vbMonday), Date), "dd.mm.yy") & "." & " worksheet not found" & _
        " in this workbook.", vbInformation, "Warning"

End Sub
You are a godsend! Thanks so much for this mate- it works now I just had to remove the & "." that I had erroneously typed in my initial question. Thanks again for the assistance, it is greatly appreciated.
 
Upvote 0
You are a godsend! Thanks so much for this mate- it works now I just had to remove the & "." that I had erroneously typed in my initial question. Thanks again for the assistance, it is greatly appreciated.
I did wonder about the "." and I did mention it.

You may want to delete this line as it would annoy me.

VBA Code:
MsgBox Format(DateAdd("d", 1 - Weekday(Date, vbMonday), Date), "dd.mm.yy") & "." & " worksheet not found" & _
        " in this workbook.", vbInformation, "Warning"
 
Upvote 0

Forum statistics

Threads
1,224,739
Messages
6,180,674
Members
452,993
Latest member
FDARYABEE

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