Comparing two cells with VBA

lwkochis

New Member
Joined
Oct 22, 2013
Messages
12
Hi,

I would like to compare two cells in a worksheet. If cell T5 <> U5 then a message displays. I have VBA macro that handles this and its:

If Not IsEmpty(Range("U5").Value) Then
If (Range("T5").Value <> Range("U5").Value) Then MsgBox ("<Display message>")
End If

How do I continue this for the cells from T5 to T23 and U5 to U23?

Thanks for your help.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
try:
Code:
For i = 5 to 23
If Not IsEmpty(Range("U" & i)) Then
If (Range("T" & i).Value <> Range("U" & i).Value) Then MsgBox "Did you want something between these quote marks?"
End If
Next i
 
Last edited:
Upvote 0
Maybe:
Code:
    For Each c in Range("T5:T23")
        If Not IsEmpty(c) Then
            if c.Value <> c.Offset(0, 1).Value Then MsgBox ("")
        End If
    Next c
 
Upvote 0
try:
Code:
For i = 5 to 23
If Not IsEmpty(Range("U" & i)) Then
If (Range("T" & i).Value <> Range("U" & i).Value) Then MsgBox "Did you want something between these quote marks?"
End If
Next i


Not working as I want it to. It has to compare each cell, e.g. t5 and u5; t6 and u6, etc. Not the entire range.
 
Upvote 0
sorry to hijack the post but...
1) how is it not working?
2) that code is exactly what you asked.
 
Upvote 0
Not working as I want it to. It has to compare each cell, e.g. t5 and u5; t6 and u6, etc. Not the entire range.
The code I posted loops through the entire range, comparing t5 to u5 then t6 to u6 ........

If there is a value in the u column cell, and it differs from the value in the t column cell, a message box pops up. Otherwise, nothing happens.
 
Upvote 0
The code I posted loops through the entire range, comparing t5 to u5 then t6 to u6 ........

If there is a value in the u column cell, and it differs from the value in the t column cell, a message box pops up. Otherwise, nothing happens.




I am not sure why it's not working. One of the problems is that even when I enter a value in another part of the spreadsheet, other than the T or U, the message box pops up. I am placing the code in the Sheet not module. There is additional code in there as well:

Private Sub Worksheet_Change(ByVal Target As Range)

If Not Application.Intersect(Range("T5:T100"), Target) Is Nothing Then
If Target.Value = "Remote" Then
Call New_State_Taxes_Outlook
End If
End If


If Not Application.Intersect(Range("T5:T100"), Target) Is Nothing Then
If Target.Value = "Carmel" Then
Call New_State_Taxes_Outlook
End If
End If


If Not Application.Intersect(Range("R5:R100"), Target) Is Nothing Then
If Target.Value = "Yes" Then
Call Benefits_Outlook
End If
End If


For i = 1 To 23
If Not IsEmpty(Range("U" & i)) Then
If (Range("T" & i).Value <> Range("U" & i).Value) Then MsgBox "Did you want something between these quote marks?"
End If
Next i


End Sub
 
Upvote 0
Wouldn't you think that it would be good to tell us at the outset this is to be embedded in sheet event code? The way you have inserted it, it does not constrain the cell selection to any specific range, so it fires anytime any cell on the sheet is changed. You need to decide which column you want to monitor for changes and constrain the embedded piece to that column.

Also your OP indicated T5:T23 and U5:U23 which is i = 5 to 23, not i = 1 to 23.
 
Upvote 0

Forum statistics

Threads
1,216,176
Messages
6,129,314
Members
449,501
Latest member
Amriddin

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