2016-08-18 76 views
0

我有一個小組,我希望在單元更新爲包含某個值時運行。如何更新單元格值時觸發Excel VBA宏?

現在,我使用如下代碼:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) 
    If Target.Cells.Count = 1 Then 
    If Target.Value = XYZ Then 
     my_sub a, b, c 
    End If 
    End If 
End Sub 

問題現在的問題是,當我直接編輯這些細胞中的宏觀僅火災,而不是當在其它細胞的變化迫使這些細胞改變。例如,這些單元格沒有很好的定義,所以我不能硬編碼「當A5改變時」。每當我的工作簿中的任何單元格更新(手動或通過公式)以滿足自己的條件時,我都需要這樣做。

+2

究竟是不是在這裏工作?這是事實,這不能捕捉公式的變化?在這種情況下,查看「Worksheet.Calculate'事件(https://msdn.microsoft.com/en-us/library/office/ff838823.aspx)。 – Mikegrann

+2

此外,只要確保你的代碼不是你實際使用的 - 如果是,你是否想'如果target.value =「XYZ」'?什麼不適用於宏? – BruceWayne

+0

它僅在我直接編輯單元格時觸發,而不是在其他單元格中的更改強制更改時觸發。我不知道如何使它與Worksheet.Calculate一起工作,因爲沒有Target參數。不,這不是代碼,所以XYZ上的引號不是問題 – user1923052

回答

2

提供你的目標是僅與需要監測的公式單個細胞,這將工作:

Option Explicit 

Dim tarVal As Variant 

Private Sub Worksheet_Activate() 

    tarVal = ActiveSheet.Range("A1").Value ' change range parameter to the address of the target formula 

End Sub 

Private Sub Worksheet_Change(ByVal Target As Range) 

    Dim tempVal As Variant 

    tempVal = ActiveSheet.Range("A1").Value 

    If tempVal <> tarVal Then 
     tarVal = tempVal 

     ' your code here 

     MsgBox "The value of A1 has changed" ' for testing purposes only, delete later 
    End If 

End Sub 

編輯

下面的代碼適用於細胞的整個範圍,但只有在自動計算打開的情況下。如果監視的單元格不連續,則只需在定義目標範圍時使用union語句。 (在本例中,目標範圍是A1:A10)。這是假設目標範圍內只有一個公式可以一次更改其值。如果多個目標公式可以做到這一點,則刪除Worksheet_Change子程序中的Exit for

Option Explicit 

Dim tarCellCount As Long 
Dim tarRng As Range 
Dim tarVals As Variant 

Private Sub Worksheet_Activate() 

    Dim i As Long 
    Dim cll As Range 

    Set tarRng = ActiveSheet.Range("A1:A10") ' change range parameter to the addresses of the target formulas 

    tarCellCount = tarRng.Cells.count 
    ReDim tarVals(1 To tarCellCount) As Variant 

    For Each cll In tarRng 
     i = i + 1 
     tarVals(i) = cll.Value 
    Next cll 

End Sub 

Private Sub Worksheet_Change(ByVal Target As Range) 

    Dim changeBool As Boolean 
    Dim i As Long 
    Dim cll As Range 
    Dim tempVal As Variant 

    For Each cll In tarRng 
     tempVal = cll.Value 
     i = i + 1 

     If tempVal <> tarVals(i) Then 
      tarVals(i) = tempVal 
      changeBool = True 
      Exit For 
     End If 
    Next cll 

    If changeBool Then 

     ' your code here 

     MsgBox "The value of one of the cells in the target range has changed" ' for testing purposes only, delete later 
    End If 

End Sub 
-1

目標是一個可以包含更多單元格的範圍。這段代碼應該適合你。

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) 
    For Each cell In Target.Cells 
    If cell.Value = XYZ Then 
     my_sub a, b, c 
    End If 
    Next cell 
End Sub 

編輯:我知道你想火也當公式更新定義的值。如果您要檢查每個單元格,它可能會很慢,但實際上取決於文件的大小。這裏有一些代碼讓你知道如何去做。

Private Sub Workbook_SheetCalculate(ByVal sh As Object) 
    For Each cell In sh.Cells.SpecialCells(xlCellTypeFormulas).Cells 
     If cell.Value = XYZ Then 
      my_sub a, b, c 
     End If 
    Next cell 
End Sub 
1
  1. 加入你的細胞被跟蹤到一個名爲公式(命名範圍)。我用rngValue
  2. 使用一個靜態變量來跟蹤你要跟蹤的價值有多少次在此範圍內
  3. 使用Calculate事件發生時檢查是否出現次數的數量變化

代碼

Private Sub Worksheet_Calculate() 
Dim StrIn As String 
Static lngCnt As Long 
Dim lngCnt2 As Long 

StrIn = "apples" 

lngCnt2 = Application.WorksheetFunction.CountIf(Range("rngValue"), StrIn) 
If lngCnt2 <> lngCnt Then 
    lngCnt = lngCnt2 
    Call mysub 
End If 

End Sub 
+0

我喜歡使用'Static','CountIf'的乾淨優雅的方法,和「Worksheet_Calculate」事件。然而,一個缺點是,這是假定所有監控公式的價值都是相同的,但情況並非總是如此,儘管我會告訴你OP對此並不十分清楚。然而,一般來說,擁有一整套具有必須包含完全相同值的公式的單元格是不常見的,除非值是某種校驗和或類似值。 – Miqi180

相關問題