If statement with macro (advanced problem!)

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 work properly. 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 manually, the above macro works fine. But I have a formula in AG6 that is "=IF(AF6=1, 1, ""). Is there any way to launch a macro if the AG6 cell becomes 1 from the formula (ie formula is TRUE)?
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Try using Worksheet_Calculate rather than Worksheet_Change.


Code:
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] Worksheet_Calculate()
   [COLOR=darkblue]If[/COLOR] Range("AG6") = 1 [COLOR=darkblue]Then[/COLOR]
      SomeMacro
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
[COLOR=darkblue]Sub[/COLOR] SomeMacro()
MsgBox ("Hi!")
End Sub

NB both procedure should reside in the same sheet module.
 
Upvote 0
Hi,

Maybe this i've just seen in the internet

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("AG6").DirectPrecedents) Is Nothing Then
        If Range("AG6") = 1 Then
            SomeMacro
        End If
    End If
 
End Sub
 
Sub SomeMacro()
    MsgBox ("Hi!")
End Sub

M.
 
Upvote 0
I think in your case you have to use Precedents not DirectPrecedents

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("AG6").Precedents) Is Nothing Then
        If Range("AG6") = 1 Then
            SomeMacro
        End If
    End If
 
End Sub
 
Sub SomeMacro()
   MsgBox ("Hi!")
End Sub

M.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,555
Members
452,928
Latest member
101blockchains

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