Execute Macros From Formulas (Or Cell Value)

indi visual

Junior Member
Oct 15, 2010
6
0
0
I would like to execute macros if specific cells change.

I have 25 cells I would like to apply this to.

After hours upon hours of failure, I would really just like to know if this even possible.

If "Z1" equals "1", then run macro
If value is not "1" do nothing
If "Z2" equals "2", then run macro
If value is not "2" do nothing
 

wetech

Senior member
Jul 16, 2002
871
6
81
yes, you'll want to use worksheet events.
in the vba editor, double click on the worksheet that contains the cells you want to check.

At the top of the editor, there are 2 dropdown boxes, in the left box, select "Worksheet" and in the right, select "Change". This will create a new sub procedure called:

Private Sub Worksheet_Change(ByVal Target As Range)

Put the following code in the procedure:

Code:
If Intersect(Target, Range("Z1:Z2")) Is Nothing Then
    Exit Sub
Else
    Select Case Target.Address
        Case "$Z$1"
            If Target.Value = 1 Then RunMacro1
        Case "$Z$2"
            If Target.Value = 2 Then RunMacro2

    End Select
End If

this checks if the cell that has changed is within the range of cells that you want to check. it then checks which cell changed, and compares it's value to the value want to trigger the new macro. Obviously, you'll need to change the "RunMacro" parts above to the actual names of your macros.