'Run macro automatically when cell value changed (not changed by user)
Is there a way to have a macro changed when the value is changed is changed, not necessarily when a user changes the value?
For example, I have a check box that is linked to a cell "A1". When the check box is checked, A1 reads "TRUE" when it is not checked it reads "FALSE". When A1 changes between TRUE and FALSE I want a macro to run.
I have seen the Private Sub Worksheet_Change code, but that seems to only work when a user actually changes cell A1. Is there another way to automatically run the macro?
UPDATE 11/29/13 Thank you everyone for your suggestions, but unfortunately it isn't quite working out. Let me try to expand further.
I have a user form with check boxes, drop down lists, and text fields. All of the data from the user form is summarized in column B of a worksheet (which is where all the user form fields are linked). When the macro runs, some calculations happen and the user get's some numbers.
When the user changes something in the user form I want the macro to run automatically. I know there is a possibility that this can become resource intensive, but I'll deal with that problem when it comes to it. I would like to have a procedure that says if the value of any cell in range B1:B20 changes then run the macro. I believe this method is easier than telling every user form field to run the same macro because it will be easier to expand and update this form later, especially if someone else takes over maintenance.
I could just have a button that the user can click to run the macro when they are done filling in the form, but I can foresee inaccurate information because the user forgets to recalculate; this is why I would like to have it done automatically and numbers are updated in real time.
Solution 1:[1]
From what I can tell you must enumerate the cells that could be changed or act upon any cell change. Here is a sample function.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" or Target.Address = "$A$2" Then
Sheet1.Range("A3").Interior.Color = RGB(255, 0, 0)
End If
End Sub
Solution 2:[2]
Use "Application.Volatile" in the first line of your VBA function
Function A()
Application.Volatile
End Function
Solution 3:[3]
Worksheet_Change works ALWAYS, but you need to have application.enableevents=true or it wont. But i don't like the if target.address part, i prefer :
Private Sub Worksheet_Change(ByVal Target As Range)
select case target.address 'or target.row, column...
case "$A$1"
application.enableevents=false 'or the change sub will trigger again on changes!
code
application.enableevents=true
case "$A$2"
application.enableevents=false 'or the change sub will trigger again on changes!
code
application.enableevents=true
end select
end sub
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | psxls |
Solution 2 | Roger Barreto |
Solution 3 |