If statement with macro

bbot23

New Member
Joined
Oct 29, 2010
Messages
37
I google'd the code to run a macro depending on a met condition, but I can't seem to make it call the macro. Can anyone help? Here's the code that I got:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("AG6")) Is Nothing Then
Exit Sub
End If

If Range("AG6") = 1 Then
SomeMacro
End If

End Sub

Sub SomeMacro()
MsgBox ("Hi!")
End Sub

If I change AG6 to 1, nothing happens. Help?
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
I have just tried this out and it works fine, you can not get it to run by
pressing F5 or F8 in the VBA Editor , you have to go to AG6 and place a 1 in it.
 
Upvote 0
That code needs to be in the code module of the same worksheet that AG6 is in. Right-click the tab for that sheet and select View Code. That's where it goes.

It will trigger when AG6 changes, so if it's already equal to 1, you need to go into edit on the cell and press Enter.
 
Upvote 0
Hi,

Maybe you have disabled events involuntarily. Just in case, copy the code below
Code:
Sub enblEvents()
    Application.EnableEvents = True
End Sub

and with the cursor inside press F5

HTH

M.
 
Upvote 0
Here's a little update:

If I type in "1" in AG6, it works. But there's a formula in AG6 that makes AG6 equal to 1 if a condition in another cell is met. When that condition is met and AG6 changes to 1, the macro is not launched.

Help? Why doesn't it work?
 
Upvote 0
Because the Worksheet_Change event is not triggered by a formula

Try this

Private Sub Worksheet_Calculate()
If Range("A6") = 1 Then
SomeMacro
End If
End Sub


But be aware that everytime a calculation occurs, even in another cell(s), the message wil appear. Probably you dont want this...

HTH

M.
 
Last edited:
Upvote 0
Complementing my previous post.

If you tell us the formula you are using in AG6 (sorry, i wrongly wrote A6 in the previous post) maybe we can arrange the things better.

M.
 
Upvote 0
Aye, the best would be if ONLY the result changes in the cell with the formula. Here's the formula, which is a bit complicated and might not mean much honestly!

=IF(AND(F6="BUY",IF(H6>U6, AF6<AE6, AF6>AE6)),1,IF(AND(F6="SHORT",IF(H6>U6,AF6>AE6,AF6< AE6)),1," "))

I guess what I could do is make AH6 equal to 1 if AG6 =1 and that would make a formula that looks like

=IF(AG6=1, 1, "")
 
Last edited:
Upvote 0
If we could relate an user-entry in some cell with the change of the value in AG6 by the formula, maybe we can solve the problem.

Say that if the user enter a new value in F6 (or H6, U6...) , by your formula it would change the value of AG6. Then, i think, we could use the Worsheet_Change event triggered by the change in F6 (that would be the Target) and check if AG6 = 1...

Otherwise, honestly, i dont know how to solve it (maybe creating a class but this is beyond my knowledge)

M.
 
Upvote 0

Forum statistics

Threads
1,224,505
Messages
6,179,152
Members
452,891
Latest member
JUSTOUTOFMYREACH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top