Find the Difference in two cells (or Columns), and Highlight/Change Color of Difference

abro16

New Member
Joined
May 9, 2014
Messages
3
For Example:
I have a text in cell A1 "I have a cat." and in cell B1 "I have a dog." now excel formula or VBA code compare both cells and highlight the specific difference.
in above case, dog and cat should be highlight in same cell.
.
Or another example (one step ahead), if we have data in Number format.
If I have number like "123456789" in cell A1 and "128456789" in cell B1. both looks similar...
but in cell B1 there is one typo mistake at place 3.
Excel compare both cells, and highlight/or change font color/and bold, the specific difference.
in above case... 3rd place digit (in same cell) should be highlight, as in both cells it does not match.
.
Real life scenario is .. if we have data in one column with Original Number (say some Social Security Number, or Contact No.), but in column B, we have same number but with little typo mistake...
maybe somwhere one digit difference, or two digits difference, or might be more... So how to know comparing both columns.... what is actual difference.

*Remember: whole cell should not be highlighted. as we already know. both cells have minor difference.
but we want to know EXACT WHAT IS DIFFERENCE....
 

Attachments

  • Screenshot 2023-03-10 233727.png
    Screenshot 2023-03-10 233727.png
    71.8 KB · Views: 60

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi and Welcome to the MrExcel forum. Please accept my warmest greetings and sincere hope that all is well.

Your description says that your data is in column A-B, but in your image the data is in column J-K.
I prepared the macro for the data in column J-K, but if it is another, it is very easy to make the change, just adjust the columns in this line of the macro:
Rich (BB code):
With Range("J2", Range("K" & Rows.Count).End(3))

Run this macro:
VBA Code:
Sub HighlightDifference()
  Dim c As Range
  Dim j As Long
  
  With Range("J2", Range("K" & Rows.Count).End(3))
    .Font.ColorIndex = xlAutomatic
    .Font.Bold = False
    
    For Each c In .Columns(1).Cells
      For j = 1 To Len(c.Value)
        If Mid(c.Value, j, 1) <> Mid(c.Offset(, 1).Value, j, 1) Then
          With c.Characters(j, 1).Font
            .Color = vbRed
            .Bold = True
          End With
          With c.Offset(, 1).Characters(j, 1).Font
            .Color = vbBlue
            .Bold = True
          End With
        End If
      Next
    Next
  End With
End Sub

HOW TO INSTALL MACROs
------------------------------------
If you are new to macros, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. To use the macro, go back to the worksheet with your data on it and press ALT+F8, select the macro name (HighlightDifference) from the list that appears and click the Run button. The macro will execute and perform the action(s) you asked for. If you will need to do this again in this same workbook, and if you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "Yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.



----- --

Note XL2BB:
For the future, it would help greatly if you could give us the sample data in a form that we can copy to test with, rather that a picture.
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in
Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.

For example:
Dante Amor
AIJK
1
243105925428834310592542993
312456892323451245689232845
443222231049834322223109983
Hoja3



----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
 
Upvote 0
Hi and Welcome to the MrExcel forum. Please accept my warmest greetings and sincere hope that all is well.

Your description says that your data is in column A-B, but in your image the data is in column J-K.
I prepared the macro for the data in column J-K, but if it is another, it is very easy to make the change, just adjust the columns in this line of the macro:
Rich (BB code):
With Range("J2", Range("K" & Rows.Count).End(3))

Run this macro:
VBA Code:
Sub HighlightDifference()
  Dim c As Range
  Dim j As Long
 
  With Range("J2", Range("K" & Rows.Count).End(3))
    .Font.ColorIndex = xlAutomatic
    .Font.Bold = False
   
    For Each c In .Columns(1).Cells
      For j = 1 To Len(c.Value)
        If Mid(c.Value, j, 1) <> Mid(c.Offset(, 1).Value, j, 1) Then
          With c.Characters(j, 1).Font
            .Color = vbRed
            .Bold = True
          End With
          With c.Offset(, 1).Characters(j, 1).Font
            .Color = vbBlue
            .Bold = True
          End With
        End If
      Next
    Next
  End With
End Sub

HOW TO INSTALL MACROs
------------------------------------
If you are new to macros, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. To use the macro, go back to the worksheet with your data on it and press ALT+F8, select the macro name (HighlightDifference) from the list that appears and click the Run button. The macro will execute and perform the action(s) you asked for. If you will need to do this again in this same workbook, and if you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "Yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.



----- --

Note XL2BB:
For the future, it would help greatly if you could give us the sample data in a form that we can copy to test with, rather that a picture.
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in
Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.

For example:
Dante Amor
AIJK
1
243105925428834310592542993
312456892323451245689232845
443222231049834322223109983
Hoja3



----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
Amazing... working... Thank you...
but i have to convert the data, from Number to "Text"...
 
Upvote 1

Forum statistics

Threads
1,223,605
Messages
6,173,321
Members
452,510
Latest member
RCan29

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