Run macro on cell value

smoorcroft

New Member
Joined
Apr 15, 2011
Messages
2
Is there a way to have a macro run if a certain value is entered into a specific cell. For example if "17580" is entered into g2 then run a macro called Check

Thanks for any help, if it is at all possible
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
You would need to add a sheet-event macro to the sheet module to accomplish this.

Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell as Range

For Each cell in Intersect(Target, Range("G2"))
    If cell = "17580" Then Call Check
Next cell

End Sub


Right-click the tab name and choose VIEW CODE, paste that macro into the sheet module. It's active all the time.

Did you really mean "just" cell G2, or the whole G column? If it's the whole column, then:

Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell as Range

For Each cell in Intersect(Target, Range("G:G"))
    If cell = "17580" Then Call Check
Next cell

End Sub


Lastly, if you need to feed in which cell in column G got that value, then:

Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell as Range

For Each cell in Intersect(Target, Range("G:G"))
    If cell = "17580" Then Call Check(cell)
Next cell

End Sub

...where the macro Check is designed to receive a parameter:
Rich (BB code):
Sub Check(cell as Range)
 
Last edited:
Upvote 0
Hello and welcome to MrExcel

Try this in your worksheet module.

<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>  <SPAN style="color:#00007F">If</SPAN> Target.Address = "$G$2" <SPAN style="color:#00007F">Then</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> Target = 17580 <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">Call</SPAN> test <SPAN style="color:#007F00">'Change to YourMacro name</SPAN><br>  <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,946
Latest member
JoseDavid

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