VBA AutoOpen sub to worksheet # based upon the current month

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,113
Office Version
  1. 365
Platform
  1. Windows
Good EveningEveryone :),

I would like to create an AutoOpen sub that automatically opens up to a specific worksheet based upon the current month.



For example, we are currently in October, so I'd like it to open up toWorksheets(4).

If its November, I'd like it to open up to Worksheets(5).


If its December,I'd like it to open up to Worksheets(6).

If its January,I'd like it to open up to Worksheets(7).

.
. ( & so on continuing upon this consecutive pattern )
.

If its August,I'd like it to open up to Worksheets(14).

If its September,I'd like it to open up to Worksheets(15).



Can someone please help me?



Many thanks inadvance!

R/
pinaceous

 
Okay, I actually have 03 Private Sub Workbook codes that work upon opening.


I’ve also tried to designate the Post#6 code as a stand-alone code, for example from Private Sub Workbook_Open() to Sub Workbook_TabOpening() then Call it within a separate Sub Auto_Open() code but that does not seem to work.


Here are the 03 codes that I can tell, if you can let me know, what you think that would be greatly appreciated!


Code:
Private Sub Workbook_Open()
Dim OneSheet As Worksheet
For Each OneSheet In ThisWorkbook.Worksheets
    OneSheet.ScrollArea = "A1:O80"
Next OneSheet
 End Sub
Code:
Private Sub Workbook_Open()

Dim shtname As String
Dim thismonth As Long

thismonth = Month(Now())

If thismonth = 11 Or thismonth = 12 Then
thismonth = thismonth - 6
Else
thismonth = thismonth + 6
End If

Worksheets(thismonth).Select

End Sub
Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    Call Move_DV_Input_Message_To(Range(TARGET_RANGE_ANCHOR))
End Sub


Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Range("B7").Select
'ActiveSheet.Range("B7").Select
ActiveWindow.Zoom = 100
    Select Case Sh.Name
    Case Else: Call RefreshRibbon(Tag:="Xron")
    
    End Select
     
End Sub


Thank you,
Pinaceous
 
Last edited:
Upvote 0

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
You can put the two open events together like
Code:
Private Sub Workbook_Open()
    Dim OneSheet As Worksheet
    Dim shtname As String
    Dim thismonth As Long

    For Each OneSheet In ThisWorkbook.Worksheets
        OneSheet.ScrollArea = "A1:O80"
    Next OneSheet
    thismonth = Month(Now())

    If thismonth = 11 Or thismonth = 12 Then
        thismonth = thismonth - 6
    Else
        thismonth = thismonth + 6
    End If
 End Sub
 
Upvote 0
That's a big dua for me. I appreciate the clarity. Peace.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
Hi mrshl9898,


Your code is working out really well.


I have another workbook where I would like to create an AutoOpen sub that automatically opens up upon a different worksheet based from Post#1, based upon the current month.

I tried to toy around with your code from Post##2 but I can't seem to get the numbers right.


Can you help me?

For example, we are currently in October, so I'd like it to open up to Worksheets(3).


If its November, I'd like it to open up to Worksheets(5).


If its December, I'd like it to open up to Worksheets(7).



If its January, I'd like it to open up to Worksheets(9).

.
. ( & so on continuing upon this consecutive pattern )
.



If its August, I'd like it to open up to Worksheets(23).


If its September, I'd like it to open up to Worksheets(25).




Many thanks again in advance!



R/
pinaceous












Many thanks again in advance!
R/
pinaceous
 
Last edited:
Upvote 0
How about
Code:
Private Sub Workbook_Open()
    Dim Sht As Long
    
    Sht = Choose(Month(Date), 9, 11, 13, 15, 17, 19, 21, 23, 25, 3, 5, 7)
    Worksheets(Sht).Activate
End Sub
 
Upvote 0
Hey Fluff!

Thanks very much! Could you explain in words how your code works? I know what I asked and you answered but looking at the code I'm baffled. For example, how does it know October is for worksheet(9) and not lets say January? I'm good with the code just want to know the how, that's all.

Many thanks & take care!
Pinaceous
 
Upvote 0
This part
Code:
Month(Date)
will give you the month number (ie 1 to 12) which is used as the index value in the choose function & that will then return the appropriate value from the list.
So month 1 will return 9, month 2 will return 11 etc.
 
Upvote 0

Forum statistics

Threads
1,214,823
Messages
6,121,777
Members
449,049
Latest member
greyangel23

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