VBA to highlight cells if it's not matching the value of the corresponding cells in another column

Bamh1

New Member
Joined
Oct 7, 2021
Messages
40
Office Version
  1. 2016
Platform
  1. Windows
  2. Mobile
Hello,

Suppose we are comparing Columns A and C. I want values of cells in Column A highlighted if not matching the corresponding value in Column C. I am thinking something like this:

Sub highlight_cells_if_not_matching ()

Dim i as Integer
Dim j as integer
Dim nr as integer

nr=selection.rows.count

For i =nr to 1 step -1
if selection.cells(i,j).value<>selection.cells.offset (i, j+2).value Then ' this is where I have problem, I am also thinking of using smth like [isna.worksheet.function(match (
selecion.cells(i, j).interior.color=vbcyan
end if
i=i+1
Next
End sub

Could someone please help me fixing this code.

Thank you,

Shawn
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Try.
VBA Code:
Sub highlight_cells_if_not_matching()

    Dim i As Integer
    
    Dim nr As Integer
    
    nr = Selection.Rows.Count
    
    For i = nr To 1 Step -1
        If Selection.Cells(i, 1).Value <> Selection.Cells(i, 1).Offset(0, 2).Value Then
            Selection.Cells(i, 1).Interior.Color = vbCyan
        End If
    Next i
End Sub
 
Upvote 0
If the values in the Selection are not the result of formulas, you could consider this method of doing them all at once.

BTW, any reason why you wouldn't just use standard Conditional Formatting for this?

VBA Code:
Sub Diff()
  Dim a As Variant
  
  With Selection
    a = .Value
    .Value = Evaluate("if(" & .Address & "=" & .Offset(, 2).Address & ","""",1)")
    On Error Resume Next
    .SpecialCells(xlConstants, xlNumbers).Interior.Color = vbCyan
    On Error GoTo 0
    .Value = a
  End With
End Sub
 
Upvote 0
Try.
VBA Code:
Sub highlight_cells_if_not_matching()

    Dim i As Integer
   
    Dim nr As Integer
   
    nr = Selection.Rows.Count
   
    For i = nr To 1 Step -1
        If Selection.Cells(i, 1).Value <> Selection.Cells(i, 1).Offset(0, 2).Value Then
            Selection.Cells(i, 1).Interior.Color = vbCyan
        End If
    Next i
End Sub
Thank you so much HongRu for fixing the code. It worked.
 
Upvote 0
If the values in the Selection are not the result of formulas, you could consider this method of doing them all at once.

BTW, any reason why you wouldn't just use standard Conditional Formatting for this?

VBA Code:
Sub Diff()
  Dim a As Variant
 
  With Selection
    a = .Value
    .Value = Evaluate("if(" & .Address & "=" & .Offset(, 2).Address & ","""",1)")
    On Error Resume Next
    .SpecialCells(xlConstants, xlNumbers).Interior.Color = vbCyan
    On Error GoTo 0
    .Value = a
  End With
End Sub
Thank you very much, Peter. This works too. I have not tried conditional formatting on two columns. the for loop solution was the first thing that came to mind, and I know it's not fastest way in terms of processing time.
 
Upvote 0
You're welcome. Thanks for the follow-up.

BTW, does this really need to rely on the user's 'Selection' or is it certain fixed columns?
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,823
Members
449,049
Latest member
cybersurfer5000

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