如果用户选择的单元格不在一系列命名范围内,则VBA结束

时间:2013-02-15 19:50:37

标签: excel-vba named-ranges select-case vba excel

我遇到了一些Select Case的问题。我的程序使用命名范围。如果select case不在一系列命名范围内,我希望它结束​​。这是我的代码在用户选择有效单元格时正确运行:

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

所以基本上如果它不在上述任何范围内,我希望程序结束。我相信有更好的方法可以做我正在尝试的事情,但我正在自学这一切,所以我确信它并不完美。

1 个答案:

答案 0 :(得分:2)

你可以尝试这样的事情( 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