Auto initiate a Macro

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I wonder if we can intiate a macro by by proving a condition
e.g. If A1=1 then run the "XYZ" Macro, if A1=0 then do not run a macro
Can we apply the same in IF function

Thanks in advance

Haren
 
You can't use a worksheet function to initiate a macro, but you can use
an Event Macro (See

http://cpearson.com/excel/events.htm

for more info).

Put this code in your worksheet code module (right click on the
worksheet tab and select "View code"):

Private Sub Worksheet_Calculate()
If Range("A1").Value = 1 Then XYZ
End If

This assumes that A1 has a formula. If A1 is instead a user entry, try
the Worksheet_Change() event instead:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Address(False, False) = "A1" Then _
If Target.Value = 1 Then XYZ
End Sub
 
Back
Top