2013-02-15 25 views
1

我遇到了一些Select Case問題。我有我的程序使用命名範圍。如果select case不在一系列命名範圍內,我希望它結束​​。這裏是我的代碼是當用戶選擇一個有效的單元格的正確運行:如果用戶選擇的單元格不在一系列命名範圍內,則VBA結束

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

你可以嘗試這樣的事情(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 
+0

這是清潔謝謝。我注意到了這個部分:在過去的案例中,這一部分現在並沒有實現。它任務退出。發生瞭解一個原因?這是否是在7,9列? – user2044180 2013-02-15 21:14:55

+0

如果列號os不是7或9,那麼它將不會執行任何操作,只需退出子程序 – 2013-02-15 21:17:56

+0

稍後在我的代碼中,它必須在我的錯誤檢查調用中有效。生病繼續挖掘。我認爲它與我的選擇錯誤檢查,我確保用戶選擇一個單元格。 '如果selection.cells.count> 1然後結束'可能會強制它 – user2044180 2013-02-15 21:25:18

相關問題