I've a question similar to the one on the post VBA macro to compare two columns and color highlight cell differences.
I used it as reference point, but now I'm stuck for hours solving my case. Code included below, I'll explain my case first for better understanding and to be easier to follow.
Case: I've the following Worksheet before any manipulation. I'm comparing the columns "A:B" and "D:E", etc (from row 3 until the last used row). See the screenshot below for a better visualization (this is just part of the data).
Now I would like to see 2 actions performed:
- Highlight the cells in A column and D column that are not part of the B and E column - I'll refer to these cells as errors
- Copy the value of the errors (highlighted cell (from A and D)) into the C and F column (this is the "Review column" - which is always 2 columns to the right in relation to the initial column)
See the screenshot below for a better visualization
CODE:
Sub compare_cols()
Dim Report As Worksheet
Dim i As Integer, j As Integer
Dim lastRow As Integer
Set Report = Excel.Worksheets("Check_Sheet")
lastRow = 80
arrInputCheckSheet= Array("A", "D", "G", "J", "M", "P", "S", "V", "Y") 'I will use these columns to compare against the next array
arrMDCheckSheet = Array("B", "E", "H", "K", "N", "Q", "T", "W", "Z") 'I will use these columns as reference
Application.ScreenUpdating = False
For a = LBound(arrInputCheckSheet) To UBound(arrInputCheckSheet)
For i = 3 To lastRow
For j = 3 To lastRow
If Report.Cells(i, arrInputCheckSheet(a)).Value <> "" Then 'This will omit blank cells at the end (in the event that the column lengths are not equal.
If InStr(1, Report.Cells(j, arrMDCheckSheet(a)).Value, Report.Cells(i, arrInputCheckSheet(a)).Value, vbTextCompare) > 0 Then
Report.Cells(i, arrInputCheckSheet(a)).Interior.Color = RGB(156, 0, 6) 'Dark red background
Report.Cells(i, arrInputCheckSheet(a)).Font.Color = RGB(255, 199, 206) 'Light red font color
Exit For
Else
End If
End If
Next j
Next i
Next a
Application.ScreenUpdating = True
End Sub
Problem:
- I'm trying to highlight the error cells with dark red background. but this code is doing exactly the opposite (is highlighting the values that match).
- How can I make the error value (the one that gets highlighted) appear in the the "check column".
I really appreciate any suggestion and support you can give to me
Thank you very much and have a nice day