VBA code doesn't run when cell is changed by a formula

匿名 (未验证) 提交于 2019-12-03 02:08:02

问题:

Worksheet A has ranges of data that are collected from Worksheet B. Worksheet A has a macro that calculates if the data is above a value then calls an email module to email selected users.

When the data is manually input on Worksheet A the Macro works, however when data is pulled from Worksheet B it doesn't fire.

I'm not sure what I need to change in my VBA code.

Private Sub Worksheet_Change(ByVal Target As Range)     Call MailAlert(Target, "B5:M5", 4)      Call MailAlert(Target, "B8:M8", 7)      Call MailAlert(Target, "B11:M11", 6)     Call MailAlert(Target, "B14:M14", 2)      Call MailAlert(Target, "B17:M17", 4)      Call MailAlert(Target, "B20:M20", 1)      Call MailAlert(Target, "B23:M23", 3)      Call MailAlert(Target, "B26:M26", 1)      Call MailAlert(Target, "B29:M29", 5)      Call MailAlert(Target, "B32:M32", 1)      Call MailAlert(Target, "B35:M35", 7)      Call MailAlert(Target, "B38:M38", 20)      Call MailAlert(Target, "B41:M41", 0)  End Sub  Private Sub MailAlert(ByVal Target As Range, ByVal Address As String, ByVal Value As Integer)     If Target.Cells.Count > 1 Then Exit Sub     If Not Application.Intersect(Range(Address), Target) Is Nothing Then         If IsNumeric(Target.Value) And Target.Value > Value Then         Call Mail_small_Text_Outlook         End If         Application.EnableEvents = True     End If End Sub 

回答1:

To capture the changes by a formula you have to use the Worksheet_Calculate() event. To understand how it works, let's take an example.

  1. Create a New Workbook.
  2. In Sheet1 Cell A1, put this formula =Sheet2!A1+1

Now In a module paste this code

Public PrevVal As Variant 

Paste this in the Sheet Code area

Private Sub Worksheet_Calculate()     If Range("A1").Value  PrevVal Then         MsgBox "Value Changed"         PrevVal = Range("A1").Value     End If End Sub 

And lastly in the ThisWorkbook Code area paste this code

Private Sub Workbook_Open()     PrevVal = Sheet1.Range("A1").Value End Sub 

Close and Save the workbook and reopen it. Now Make any change to the cell A1 of Sheet2. You will notice that you will get the message box MsgBox "Value Changed"

SNAPSHOTS



回答2:

The worksheet_change event will only fire on manual user changes. I think your best bet would be to implement this as a worksheet change event on your Worksheet B, where I presume the user input changes are taking place.

There are some alternatives which I'll suggest if this really doesn't suit you, but I think this is probably by far the best option.

Edit: Another suggestion per following comments

The ThisWorkbook object has an event SheetChange, which will be fired any time any sheets in the workbook are changed. If you can identify the ranges where data will be entered on each of the B sheets you can then use these ranges as in your original code.

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)     If Not Sh Is Sheets("Worksheet A") Then         If Intersect(Sh.Range("B1:B5"), Target) Then             'Call MailAlert as required here         ElseIf Intersect(Sh.Range("B10:B20"), Target) Then             'Call MailAlert as required here         Else ' Etc...             'Call MailAlert as required here         End If     End If End Sub 

Let me know how that goes.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!