How 2 assign a macro when a cell changes words

Alpacino

Well-known Member
Joined
Mar 16, 2011
Messages
511
How do u assign a macro when a value in a cell changes. I would cell A1 to run macro 2 when cell A1 = Green

Thanks
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Is there a formula in cell A1 or a drop-down list or does the user type in "Green" in cell A1? How does cell A1 get changed. It would matter in this case.

If cell A1 has a Drop-down list or you type in green, then this method would work.

Right-click on the sheet tab and select View Code from the pop-up menu. Paste the code below in the VBA edit window.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$A$1" Then
        If LCase(Target.Value) = "green" Then macro2
    End If
End Sub

If Cell A1 has a formula, then what is the formula?
 
Upvote 0
Maybe this could suit:

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_Change(<SPAN style="color:#00007F">ByVal</SPAN> Target <SPAN style="color:#00007F">As</SPAN> Range)<br><br><SPAN style="color:#00007F">Dim</SPAN> Changed <SPAN style="color:#00007F">As</SPAN> Range<br><SPAN style="color:#00007F">Set</SPAN> Changed = Intersect(Target, Range("A1"))<br><br>    <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> Changed <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>        <SPAN style="color:#00007F">If</SPAN> UCase(Target.Value) = "GREEN" <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">Call</SPAN> Macro2<br>    <SPAN style="color:#00007F">Else</SPAN><br>    <br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
Thanks for reply. A1 is a result from an if formula. =If(D1>60,"Green","Red") does that help??
 
Upvote 0
Thanks for reply. A1 is a result from an if formula. =If(D1>60,"Green","Red") does that help??

If the user types in a value in D1, then try this...
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "[COLOR="Red"]$D$1[/COLOR]" Then
        If LCase(Range("[COLOR="Red"]A1[/COLOR]").Value) = "green" Then macro2
    End If
End Sub

This would trigger when D1 is changed by the user but test the result for "Green" in A1.
 
Upvote 0
Alternatively, you could put the A1 formula in the code and test if D1 > 60

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "[COLOR="Red"]$D$1[/COLOR]" Then
        If [COLOR="Red"]Target.Value > 60[/COLOR] Then Macro2
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,248
Members
452,900
Latest member
LisaGo

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