2017-07-30 124 views
1
Private Sub Worksheet_Change(ByVal Target As Range) 
    Dim KeyCells As Range 

    ' The variable KeyCells contains the cells that will 
    ' cause an alert when they are changed. 
    Set KeyCells = Range("z12:z15") 

    If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then 
     If Range("Z12:Z45).value = "yes" then 
      MsgBox "Cell " & Target.Address & " has changed." 
     End If 
    End If 
End Sub 

我有問題,解決這個問題。任何幫助,將不勝感激。 TXVBA excel,在單元格範圍內運行=「是」的宏

路易吉

+0

您需要檢查「目標」(更改)範圍內的任何單元格是否具有「是」? –

+0

這是正確的。此代碼現在給我錯誤。 –

+0

_Target_是一個範圍...使用'... Application.Intersect(KeyCells,Target)...' – jsotola

回答

4

在VBA中,你不能比較的陣列(Range("Z12:Z45").value)以恆定的(「是」)或任何其他爲此事。您需要循環範圍的單元格(或數組的條目),或者可能使用MatchCountIf函數。

此外,要檢查更改,您需要檢查Target範圍,而不是Range("z12:z15")。以下是如何通過一個循環完成它:

Private Sub Worksheet_Change(ByVal Target As Range) 
    Dim KeyCells As Range, cel As Range 

    ' The variable KeyCells contains the cells that will cause an alert when they are changed. 
    Set KeyCells = Range("z12:z15") 

    If Not Intersect(KeyCells, Target) Is Nothing Then 
     For Each cel In Intersect(KeyCells, Target) 
     If StrComp(cel.text, "yes", vbTextCompare) = 0 Then 
      MsgBox "Cell " & cel.Address & " has changed." 
     End If 
     Next 
    End If 
End Sub 
+1

就像一個魅力!感謝您分享你的知識。 –

相關問題