VBA weekday function

Trudzia84

New Member
Joined
Mar 4, 2023
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi
In cell A1 I have a date. I want to write a simple VBA code which will give the warning message box if the date in A1 is changed to weekend day. The code I wrote is not working and I am not sure what is wrong with it. Any suggestions?
IMG_20230304_144726~2.jpg
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Welcome to the Board!

The way you have written it, it thinks "A1" is a variable.
Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$A$1" And Weekday(Target.Value, vbMonday) > 5 Then
        MsgBox "ggggg"
    End If
End Sub

Also, in the future, please do not post images of your code. We cannot copy that to our side. See here for how to post your code:
 
Upvote 0
Welcome to the Board!

The way you have written it, it thinks "A1" is a variable.
Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$A$1" And Weekday(Target.Value, vbMonday) > 5 Then
        MsgBox "ggggg"
    End If
End Sub

Also, in the future, please do not post images of your code. We cannot copy that to our side. See here for how to post your code:
The code works as I wanted. There is only one problem I noticed: now if I change any cell value in the spreadsheet from numer to text I get VBA run time error 13, Type mismatch. Is there a way to fix this?
 
Upvote 0
Break up your IF into 2 so that it does not evaluate the date unless you are in cell A1, i.e.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$A$1" Then
        If Weekday(Target.Value, vbMonday) > 5 Then
            MsgBox "ggggg"
        End If
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,086
Messages
6,123,038
Members
449,092
Latest member
ikke

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