Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel Macro to recognize what day of the week today is [duplicate]

Tags:

excel

vba

I'd like to create a macro that will look at what day of the week today is (Monday, Tuesday, Wednesday, etc.) and if the day is a saturday or a sunday then the macro will just stop everything but if it is a weekday (M,T,W,Th,or F) then it will call the next Sub.

Private Sub Workbook_Open()
    **If today is Saturday or Sunday Then**
        **do nothing**
        Else
        Call Populate_Workbook()
      End If
End Sub

the parts with the ** are the parts that I need help figuring out. If someone ends up figuring this out, can you explain how you came up with the solution? Trying to learn and understand macros more.

like image 773
THAT newbie Avatar asked Feb 10 '23 04:02

THAT newbie


1 Answers

Here you go...

Private Sub Workbook_Open()
    If Weekday(Date, vbMonday) < 6 Then Populate_Workbook
End Sub

The Weekday() function has an optional 2nd parameter that tells it which day of the week the week starts on. The Weekday() function returns an integer from 1 through 7.

I've instructed it to start the week on MONDAY and so SATURDAY would be 6 and SUNDAY would be 7.

like image 197
Excel Hero Avatar answered May 23 '23 02:05

Excel Hero