Search code examples
excelnamed-rangesselect-casevba

VBA End if user selected cell is not within a series of named ranges


I am having trouble with a bit of Select Case. I have my program working with named ranges. I want it to End if the select case is not in a series of named ranges. Here is my code that is running correctly for when the user selects a valid cell:

Private Sub WorkSheet_SelectionChange(ByVal Target As Range)

If Not Intersect(Target, Range("ActionDisplay")) Is Nothing Then
    Select Case Target.Column
        Case 5: Call Find_Action_RPI_Info
        Case 6: Call Find_Action_RPI_Info
        Case 7: Call Find_Action_RPI_Info
        Case 8: Call Find_Action_RPI_Info
        Case 9: Call Find_Action_RPI_Info
        Case 10: Call Find_Action_RPI_Info
        Case 11: Call Find_Action_RPI_Info
        Case 12: Call Find_Action_RPI_Info
        Case Else
    End Select
End If

If Not Intersect(Target, Range("ActionTotalDisplay")) Is Nothing Then
    Select Case Target.Column
        Case 13: Call Action_Total_RPI_Info
        Case Else
    End Select
End If

If Not Intersect(Target, Range("TotalDisplay")) Is Nothing Then
    Select Case Target.Column
        Case 5: Call CM_Action_Total_RPI_Info
        Case 6: Call CM_Action_Total_RPI_Info
        Case 7: Call CM_Action_Total_RPI_Info
        Case 8: Call CM_Action_Total_RPI_Info
        Case 9: Call CM_Action_Total_RPI_Info
        Case 10: Call CM_Action_Total_RPI_Info
        Case 11: Call CM_Action_Total_RPI_Info
        Case 12: Call CM_Action_Total_RPI_Info
        Case Else
    End Select
End If

If Not Intersect(Target, Range("GroupTotal")) Is Nothing Then
    Select Case Target.Column
        Case 13: Call GroupDisplay
    Case Else
    End Select
End If


If Not Intersect(Target, Range("PastDue")) Is Nothing Then
    Select Case Target.Column
        Case 7: Call PastDueDisplay
        Case 8: Exit Sub
        Case 9: Call PastDueDisplay
        Case 10: Exit Sub
    Case Else
    End Select
End If

If Not Intersect(Target, Range("PastDueTotal")) Is Nothing Then
    Select Case Target.Column
        Case 7: Call PastDueTotalDisplay
        Case 8: Exit Sub
        Case 9: Call PastDueTotalDisplay
        Case 10: Exit Sub
    Case Else
    End Select
End If


End Sub

So basically if its not in any of the above ranges I want the program to end. I am sure there are better ways to do what I am attempting but I am teaching myself all this so I am sure its not perfect.


Solution

  • You can try something like this (UNTESTED)

    Private Sub WorkSheet_SelectionChange(ByVal Target As Range)
        If Not Intersect(Target, Range("ActionDisplay")) Is Nothing Then
            Select Case Target.Column
                Case 5 To 12: Call Find_Action_RPI_Info
            End Select
        ElseIf Not Intersect(Target, Range("ActionTotalDisplay")) Is Nothing Then
            If Target.Column = 13 Then Call Action_Total_RPI_Info
        ElseIf Not Intersect(Target, Range("TotalDisplay")) Is Nothing Then
            Select Case Target.Column
                Case 5 To 12: Call CM_Action_Total_RPI_Info
            End Select
        ElseIf Not Intersect(Target, Range("GroupTotal")) Is Nothing Then
            If Target.Column = 13 Then Call GroupDisplay
        ElseIf Not Intersect(Target, Range("PastDue")) Is Nothing Then
            Select Case Target.Column
                Case 7, 9: Call PastDueDisplay
            End Select
        ElseIf Not Intersect(Target, Range("PastDueTotal")) Is Nothing Then
            Select Case Target.Column
                Case 7, 9: Call PastDueTotalDisplay
            End Select
        End If
    End Sub