Macro to check what was just typed in.

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,
I'm not sure how t do this but I want a way to check what has just been typed it

I have a range "AI9:AI10000"

And "AH9:AH10000"

Id like a macro that when some type anything into a cell of "AI9:AI10000" it checks to see if it Higher or equal to the value in cell "AH9:AH10000"
if it is great, if not message box "The value must be higher than the sale price!, Please try again, and clear it and select it.

please help with this if you can
Thanks
Tony
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi, Tony.
Please, try a copy of this code in the worksheet module.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 If Target.Count > 1 Then Exit Sub
 If Intersect(Target, [AI9:AI10000]) Is Nothing Or Target.Value = "" Then Exit Sub
 If Target.Value < Target.Offset(, -1).Value Then
  MsgBox "The value must be higher than the sale price!, Please try again"
  Target.Value = ""
 End If
End Sub
 
Upvote 0
Solution
Hi to all.
@Osvaldo Palmeiro, I would add these other two lines of code to avoid redundante triggering of the event Worksheet_Change, here:
VBA Code:
Application.EnableEvents = False
Target.Value = ""
Application.EnableEvents = True
 
Upvote 0
Hi to all.
@Osvaldo Palmeiro, I would add these other two lines of code to avoid redundante triggering of the event Worksheet_Change, here:
VBA Code:
Application.EnableEvents = False
Target.Value = ""
Application.EnableEvents = True
Well, I think it's not necessary since if Target is empty then the execution will be terminated. ~~~> Or Target.Value = "" Then Exit Sub
I prefer this way as it eliminates the risk that if the execution is interrupted for whatever reason, not all users have knowledge enough to reset EnableEvents.
 
Last edited:
Upvote 0
Well, I think it's not necessary since if Target is empty then the execution will be terminated.
Event Worksheet_Change triggers whatever change is made in a cell, even if emptied like in this case.
I prefer this way as it eliminates the risk that if the execution is interrupted for whatever reason, not all users have knowledge enough to reset EnableEvents.
This instead is a reason that justifies a couple of extra cycles :).
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,618
Members
449,092
Latest member
amyap

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