I need to write a VBA code that checks if date is Saturday or Sunday. If it is, a Pop Up…
- User
I need to write a VBA code that checks if date is Saturday or Sunday. If it is, a Pop Up appears saying "No schedules for the weekend" and on OK, it exists the sub and stops the code from running
- Assistant
Here's an example code that does what you described:
Sub CheckWeekend() Dim checkDate As Date checkDate = Date ' use the current date If Weekday(checkDate) = vbSaturday Or Weekday(checkDate) = vbSunday Then MsgBox "No schedules for the weekend" Exit Sub ' stop the code from running End If ' add your rest of the code here End SubYou can modify the
checkDatevariable to use a different date if needed. TheWeekday()function returns the day of the week as a number (1 = Sunday, 2 = Monday, etc.) and we check if it's equal tovbSaturdayorvbSunday, which are constants defined for those days of the week.If the date is a weekend day, a message box is displayed with the given text, and the
Exit Substatement stops the code from running any further. If it's not a weekend day, the rest of the code can be added in the space provided.