PDA

View Full Version : [SOLVED:] Worksheet SelectionChange Error - Target.Address - if statement



anish.ms
08-08-2022, 12:40 AM
Hi,

Could somebody please help me with the below
why the below if statement is not working?


If Target.Address <> "$D$7" Or Target.Address <> "$M$5" Or Target.Address <> "$M$6" Then Exit Sub
ShowCalendar
End Sub


Do I really need the following select statement?



Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Select Case Target.Address
Case "$D$7"
ShowCalendar
Case "$M$5"
ShowCalendar
Case "$M$6"
ShowCalendar
Case Else
Exit Sub
End Select
End Sub

rollis13
08-08-2022, 02:32 AM
This would work:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$D$7" Or Target.Address = "$M$5" Or Target.Address = "$M$6" Then showcalendar
End Subor:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address <> "$D$7" And Target.Address <> "$M$5" And Target.Address <> "$M$6" Then Exit Sub
showcalendar
End Subor:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Union(Range("$D$7"), Range("$M$5"), Range("$M$6"))) Is Nothing Then showcalendar
End Sub

anish.ms
08-08-2022, 02:52 AM
thanks rollis

rollis13
08-08-2022, 07:44 AM
Glad I was able to help :).

jolivanes
08-15-2022, 10:31 PM
Or change what you had slightly

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Select Case Target.Address
Case "$D$7", "$M$5", "$M$6"
ShowCalendar
Case Else
Exit Sub
End Select
End Sub

anish.ms
08-15-2022, 11:58 PM
Thanks jolivanes (http://www.vbaexpress.com/forum/member.php?1993-jolivanes)