【问题标题】:VBA End if user selected cell is not within a series of named ranges如果用户选择的单元格不在一系列命名范围内,则 VBA 结束
【发布时间】:2013-02-15 19:50:36
【问题描述】:

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

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

【问题讨论】:

    标签: excel named-ranges select-case vba


    【解决方案1】:

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

    【讨论】:

    • 这更干净,谢谢。我注意到了这个部分:在过去的案例最后,那个部分现在不起作用。它只是退出。碰巧知道原因吗?是因为 7、9 的列吗?
    • 如果列号 os 不是 7 或 9 则它不会做任何事情并简单地退出子
    • 它一定是我稍后在我的代码中的错误检查调用中的东西。生病继续挖。我认为它通过我的选择错误检查来确保用户选择一个单元格。 'IF selection.cells.count>1 then end' 这可能会迫使它退出
    • 我删除了错误检查,这个解决方案运行正常。如果用户选择多个单元格,我仍然希望有一个安全网来退出程序。现在,如果用户选择多个单元格,它将为选择中的最后一个单元格运行程序。谁能给我一点时间来告诉我如何防止这种情况发生?
    猜你喜欢
    • 1970-01-01
    • 2015-08-01
    • 1970-01-01
    • 2018-10-12
    • 2015-12-26
    • 1970-01-01
    • 2018-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多