VBA to search column when cells change

DrH100

Board Regular
Joined
Dec 30, 2011
Messages
78
Hi all
Hope someone can help with what I trying to do.

What I would like (and have no idea where to start) is when a user updates a cell in my worksheet that the code looks at the cells at the top of that column and if one is greater than the other produces a message box.

Ie: User updates cell AA33 (or any cell in that column). If cell AA1 is greater than cell AA2 a message is displayed. If it's not all is good until the next cell is changed.

I would like this to happen no matter what column the user inputs in although the max range is likely to be something like E28 to BB128 if that helps.

Any help appreciated, as I say I have no idea where to start or even if my description makes any sense.

Thanks in advance.
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Yes, that can be done with a Worksheet_Change event procedure, which is VBA code that runs automatically when a cell is updated manually.
However, I think we need a little clarification. When a cell is updated, are we always comparing the value of row 1 in that column to row 2?
If not, please explain the exact logic of how we can determine which cells we are comparing.
 
Upvote 0
Thanks Joe4

That's correct. It will always be the 2 cells at the top of the column the user updates. Essentially those cell contain various counts of the information in the column.

Hope that makes sense.

Thanks again
 
Upvote 0
Right-click on the sheet tab name at the bottom of the screen, select View Code, and then paste this VBA code in the resulting VB Editor window:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim myRange As Range
    Dim col As Long

'   Set range to apply this to
    Set myRange = Range("E28:BB128")

'   Exit if more than one cell updated at a time
    If Target.CountLarge > 1 Then Exit Sub
    
'   Exit if cell updated is outside of designated range
    If Intersect(Target, myRange) Is Nothing Then Exit Sub
    
'   Get number of updated column
    col = Target.Column
    
'   Check to see if row 1 is greater than row 2 in that column
    If Cells(1, col) > Cells(2, col) Then
        MsgBox "Row 1 is greater than row 2 in this column"
    End If

End Sub
This should do what you want automatically when you manually update a cell in the range E28:BB128.
 
Upvote 0
You are welcome.
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,030
Members
448,940
Latest member
mdusw

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