Autorun Macro

  • Thread starter Thread starter RK
  • Start date Start date
R

RK

How do I get a macro to auto run (not on starting excel, but at any time).

Say whenever the value in cell A1<5, automatically Call Macro1
 
Hi
you can use the worksheet_change event. Put the following in your
worksheet module:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Intersect(Target, Me.Range("A1")) Is Nothing Then Exit Sub
On Error GoTo CleanUp:
With Target
If .Value < 5 Then
Application.EnableEvents = False
' insert your code
End If
End With
CleanUp:
Application.EnableEvents = True
End Sub
 
The best answer would depend on how A1 gets to be less than 5

If it is by someone editing the cell, then use the change event. If it is
by calculation or from a DDE link, then use the Calculate event

See Chip Pearson's page on events
http://www.cpearson.com/excel/events.htm

If using the calculate event, you would also need to consider what happens
after the cell first achieves the tested condition and the macro is run once
for that event.
 
Back
Top