Run macro when enter key is pressed

themoreiseeyou

New Member
Joined
Apr 22, 2017
Messages
22
Hi

I would like a Sub to run every time the number in a particular cell changes. Could someone help me with the syntax?

The follwing does not work:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(False, False) = "M3" Then Call Search
End Sub


Thanks in advance
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  11/29/2018  7:26:51 AM  EST
If Not Intersect(Target, Range("M3")) Is Nothing Then
If Target.Cells.CountLarge > 1 Then Exit Sub
Call Search
End If
End Sub
 
Last edited:
Upvote 0
Hi

Your solution works if the number in the cell is changed manually. However, the number in cell "M3" is changed using the following formula: =IF(AD57=1,RANDBETWEEN(1,8))
When this formula is placed in cell M3 the Private Sub does not work. Have you any idea how I can overcome this?

Thanks in advance
 
Upvote 0
Hi,

You could test the Private Sub Worksheet_Calculate() event macro..

Hope this will help
 
Upvote 0
That's true. These type scripts only run when a manual change is made. To have it work on a formula change would require help from someone else here on the forum.
Hi

Your solution works if the number in the cell is changed manually. However, the number in cell "M3" is changed using the following formula: =IF(AD57=1,RANDBETWEEN(1,8))
When this formula is placed in cell M3 the Private Sub does not work. Have you any idea how I can overcome this?

Thanks in advance
 
Upvote 0
Hi

Could this sub be altered so that: if cell M3 contains the number 2 then "Call Search" whenever the enter key is pressed?

Thanks in advance

Private Sub Worksheet_Change(ByVal Target As Range)
'Modified 11/29/2018 7:26:51 AM EST
If Not Intersect(Target, Range("M3")) Is Nothing Then
If Target.Cells.CountLarge > 1 Then Exit Sub
Call Search
End If
End Sub
 
Upvote 0
Try this:
Now again the value 2 must be entered manually not as a result of a formula.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  11/29/2018  8:27:26 AM  EST
If Not Intersect(Target, Range("M3")) Is Nothing Then
If Target.Cells.CountLarge > 1 Then Exit Sub
If Target.Value = 2 Then
Call Search
End If
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,160
Members
453,021
Latest member
Justyna P

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