How do I run a macro from a event structure?

  • Thread starter Thread starter SLE
  • Start date Start date
S

SLE

I have a macro and I can off cause run it from "Alt + F8". But how can I run
the mecro each time the cell B2 is changing value?
 
I have a macro and I can off cause run it from "Alt + F8". But how can I run
the mecro each time the cell B2 is changing value?

Put following code in your worksheet module:

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = "$B$2" Then
YourMacro
End If

End Sub

If you have more than one cell you could also use a select case
statement.

hth

Carlo
 
How does B2 get changed?

Manually or by calculation?

Event code can be used but type of event is crucial for an answer.


Gord Dibben MS Excel MVP
 
B2 is changed by calculation.
Let us say B2 is "=B1".
If I Manualy change the value in B1, then the value in B2 is changed also.
This value change in B2 must create the event that runs the macro.
 
I've been away a couple of days.

I am surprised you have not received an answer.

Try this in the sheet module.

Private Sub Worksheet_Calculate()
On Error GoTo stoppit
Application.EnableEvents = False
With Me.Range("B2")
If .Value <> "" Then
Call themacroname
End If
End With
stoppit:
Application.EnableEvents = True
End Sub


Gord
 

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

Back
Top