1

我遇到了一些 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

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

4

1 回答 1

2

您可以尝试这样的事情(测试)

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
于 2013-02-15T19:55:10.527 回答