using the macro in Worksheet function

J

jannot

Hi,
I try to use the macro in one condition of "IF"
If(B1=4,Macro1,"not necessary")
I don't know if is posibile run Macro1.
somebody can help me ?

Thanks in advance.
 
B

Bernard Liengme

A function can do one thing: it can return a value
It cannot run a macro
best wishes
 
J

Jacob Skaria

As Bernard mentioned the macro cannot be trigerred from a formula.
However you can make use of the worksheet change event to call the macro
when B4 is 4. Try the below. From sheet tab>View code and paste the below
code...now if the value in B4 is entered as 4 macro1 is initiated.

Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Application.Intersect(Target, Range("B4")) Is Nothing Then
If Target.Value = 4 Then Call Macro1
End If
Application.EnableEvents = True
End Sub

If this post helps click Yes
 
G

Gord Dibben

If B4 is a calculated value use

Private Sub Worksheet_Calculate() event instead of Change event.


Gord Dibben MS Excel MVP
 

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