Consulting

Results 1 to 6 of 6

Thread: Worksheet SelectionChange Error - Target.Address - if statement

  1. #1
    VBAX Mentor
    Joined
    Nov 2020
    Location
    Cochin, Kerala
    Posts
    314
    Location

    Worksheet SelectionChange Error - Target.Address - if statement

    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

  2. #2
    VBAX Contributor rollis13's Avatar
    Joined
    Jun 2013
    Location
    Cordenons
    Posts
    146
    Location
    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 Sub
    or:
    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 Sub
    or:
    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
    Last edited by rollis13; 08-08-2022 at 02:44 AM.

  3. #3
    VBAX Mentor
    Joined
    Nov 2020
    Location
    Cochin, Kerala
    Posts
    314
    Location
    thanks rollis

  4. #4
    VBAX Contributor rollis13's Avatar
    Joined
    Jun 2013
    Location
    Cordenons
    Posts
    146
    Location
    Glad I was able to help .

  5. #5
    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

  6. #6
    VBAX Mentor
    Joined
    Nov 2020
    Location
    Cochin, Kerala
    Posts
    314
    Location
    Thanks jolivanes


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •