Run VBA when cell equals a value

ken1921

New Member
Joined
Aug 21, 2017
Messages
21
Hi

I want to run some VBA when a cell equals a certain value, for e.g. A1 = 1.

I've looked around the net and found code examples using but can't get it to work

All help appreciated
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Sorry that was meant to say I've looked around the net and found code examples using Worksheet_Change(ByVal Target As Range) but can't get it to work
 
Upvote 0
Try
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then End
If Target = Range("A1") And Range("A1") = "1" Then
    'code you want to run here

End If
 
End Sub
 
Last edited:
Upvote 0
Tried this and nothing happens. Not sure the code is running - I put a break point in but it never reached it
 
Upvote 0
the code need to be on the sheet. right click sheet name - view code, then paste code in.
 
Upvote 0
Many thanks

That now works except that it runs whenever I enter 1 anywhere on the sheet and not just in A1
 
Upvote 0
@ken1921:

In the worksheet's module, try:
Rich (BB code):
Option Explicit
  
Private Sub Worksheet_Change(ByVal Target As Range)
  
  If Target.Count + Target.Column + Target.Row = 3 And Me.Range("A1").Value = 1 Then
    MsgBox "Code you want to run here or a call to a procedure with the code..."
  End If
  
End Sub
Hope that helps.
@Scott T:

I would respectfully recommend against using the End Statement like that. A simple Exit Sub would suffice without destroying variables values, etc.


Mark
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,262
Members
452,627
Latest member
KitkatToby

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