Auto initiate a Macro

H

Haren Jethwa

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
 
J

JE McGimpsey

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top