Code To Highlight What Is In Column AD On Sheet 2 But Not In Column AD on Sheet 1 With K As The Delimiter

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,783
Office Version
  1. 365
Platform
  1. Windows
I have hidden a lot of columns for clarity.

The code first needs to concentrate on column K. It needs to look at the number on sheet 2 then the corresponding number on sheet 1. It then needs to look at column AD. Any numbers that are in AD on sheet 2 but not in column AD on sheet 1 then the entire row on sheet 1 needs to be highlighted please.

For clarity I have highlighted the 4 different examples a different colour. The data in yellow as you can see the 4 numbers on sheet 2 are listed ok on sheet 1 (next to the corresponding number in K M10200360000001)

With the blue data you can see there is a difference, 2910LMB & 2910RMB (I have left clear) are on sheet 2 but when you look at the data in blue on sheet 1 they are not there so all those rows would need to be highlighted (obviously they would all be clear).

The data in green is fine as they match on sheet 1.

The data in red all the numbers are missing on sheet 1 as they say NYA in all rows so those rows would need to be highlighted.

Its one of those ones where it may be harder to explain than it is to find a solution!


Excel 2010
KAD
2M102003600000012906LM
3M102003600000012907RM
4M102003600000012908LMB
5M102003600000012909RMB
6M102003600000022906LM
7M102003600000022906LM
8M102003600000022907RM
9M102003600000022907RM
10M102003600000022908LMB
11M102003600000022910LMB
12M102003600000022910RMB
13M102003600000022909RMB
14M102003600000032906LM
15M102003600000032907RM
16M102003600000032908LMB
17M102003600000032909RMB
18M102003600000042906LM
19M102003600000042907RM
20M102003600000042908LMB
21M102003600000042909RMB
Sheet2



Excel 2010
KAD
2M10200360000001NYA
3M10200360000001NYA
4M102003600000012906LM
5M102003600000012907RM
6M10200360000001NYA
7M10200360000001NYA
8M102003600000012908LMB
9M102003600000012909RMB
10M10200360000002NYA
11M10200360000002NYA
12M102003600000022906LM
13M102003600000022907RM
14M10200360000002NYA
15M10200360000002NYA
16M102003600000022908LMB
17M102003600000022909RMB
18M10200360000003NYA
19M10200360000003NYA
20M102003600000032906LM
21M102003600000032907RM
22M10200360000003NYA
23M10200360000003NYA
24M102003600000032908LMB
25M102003600000032909RMB
26M10200360000004NYA
27M10200360000004NYA
28M10200360000004NYA
29M10200360000004NYA
30M10200360000004NYA
31M10200360000004NYA
32M10200360000004NYA
33M10200360000004NYA
Sheet1
 
No I didn't. But I have now and it seems to have worked. Top man, thanks.
 
Upvote 0

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
I know its been a couple of years since the original post but all of a sudden the code has stopped working. It needs to look at column AE rather than AD now as I have an extra column. I changed everywhere that says AD to AE but its not highlighting the rows where the data differs. Is there anything else that needs to changed in Leiths code please?
 
Upvote 0
This was the working code apart from the changes of column (AE)

Code:
Option Explicit




Global Dict1 As Object
Global Dict2 As Object




Sub LoadDictionary(ByRef Dict As Object, ByRef Wks As Worksheet)


    Dim cell    As Range
    Dim cx      As Long
    Dim Data()  As Variant
    Dim Key     As String
    Dim rng     As Range
    Dim RngBeg  As Range
    Dim RngEnd  As Range
    Dim X       As Long
    
        If Dict Is Nothing Then
            Set Dict = CreateObject("Scripting.Dictionary")
            Dict.CompareMode = vbTextCompare
        Else
            Dict.RemoveAll
        End If
        
            Set RngBeg = Wks.Range("K2")
            Set RngEnd = Wks.Cells(Rows.Count, RngBeg.Column).End(xlUp)
            If RngEnd.Row < RngBeg.Row Then Exit Sub
            
            cx = Wks.Columns("K:AE").Count - 1
            Set rng = Wks.Range(RngBeg, RngEnd)
            
            For Each cell In rng
                Key = Trim(cell.Value)
                If Key <> "" Then
                    If Not Dict.Exists(Key) Then
                        ReDim Data(0)
                        GoSub UpdateData
                        ' Add the Key and Data to this dictionary.
                        Dict.Add Key, Data
                    Else
                        ' Return the Data for this Key.
                        Data = Dict(Key)
                        GoSub UpdateData
                        ' Save the updated Data for this Key.
                        Dict(Key) = Data
                    End If
                End If
            Next cell
           
Exit Sub




UpdateData:
            ' Add this cell's row number to the list of rows to highlight for this Key.
            Data(0) = Data(0) & cell.Row & ","
            
            With cell.Offset(0, cx)
                ' Check if "AD" entry is a duplicate.
                For X = 1 To UBound(Data)
                    If Data(X) = .Value Then
                        Return
                    End If
                Next X
                ' Save the value in column "AD" in element (1) onward if it is not "NYA" nor "NLA".
                If .Value <> "NYA" And .Value <> "NLA" And .Value <> "NDA" And .Value <> "NA" Then
                    ReDim Preserve Data(UBound(Data) + 1)
                    Data(UBound(Data)) = .Value
                End If
            End With
        Return




End Sub




Sub CatDiffFromSupplier()






    Dim cell        As Range
    Dim Cnt         As Long
    Dim cx          As Long
    Dim Data1       As Variant
    Dim Data2       As Variant
    Dim j           As Long
    Dim k           As Long
    Dim Key         As Variant
    Dim n           As Long
    Dim nRows       As Variant
    Dim rng         As Range
    Dim RngBeg      As Range
    Dim RngEnd      As Range
    Dim Wks         As Worksheet
     
        ' Namaes of the worksheets to compare.
        Call LoadDictionary(Dict1, ActiveWorkbook.Worksheets("Sheet1"))
        Call LoadDictionary(Dict2, ActiveWorkbook.Worksheets("Sheet2"))
        
        Set Wks = ActiveWorkbook.Worksheets("Sheet1")
        
        cx = Wks.Columns("K:AE").Count
        
        Application.ScreenUpdating = False
        
            Set RngBeg = Wks.Range("K2")
            Set RngEnd = Wks.Cells(Rows.Count, RngBeg.Column).End(xlUp)
            If RngEnd.Row < RngBeg.Row Then Exit Sub
            
            Set rng = Wks.Range(RngBeg, RngEnd)
            
            For Each Key In Dict1.Keys
                Data1 = Dict1(Key)
                Data2 = Dict2(Key)
                Cnt = 1
                
                If VarType(Data2) <> vbEmpty Then
                    ' Check the number of entries is the same.
                    If UBound(Data1) >= UBound(Data2) Then
                        ' Loop through each column "ÄD" entry for Sheet2.
                        For k = 1 To UBound(Data2)
                            ' Check Data1 entries match Data2 entries.
                            For j = 1 To UBound(Data1)
                                If Data1(j) = Data2(k) Then
                                    Cnt = Cnt + 1
                                    Exit For
                                End If
                            Next j
                        Next k
                        
                        ' Was all of Data2 found in Data1?
                        If Cnt < UBound(Data2) Then
                            GoSub HighlightRows
                        End If
                    Else
                        GoSub HighlightRows
                    End If
                End If
            Next Key
            
        Application.ScreenUpdating = True
        
Exit Sub




HighlightRows:
            nRows = Split(Data1(0), ",")
            For n = 0 To UBound(nRows) - 1
                Wks.Cells(nRows(n), "A").Resize(1, cx).Interior.ColorIndex = 3
            Next n
        Return
        
End Sub


Sub Reset()


    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
So, I understand that this works for the original setup if AE is replaced back with AD.
I will be back…
 
Upvote 0
It seemed to work fine when it was AD but after changing to AE it does not highlight the rows when the data on sheet 1 does not match sheet 2.
 
Upvote 0
I moved both columns from AD to AE and also changed the two references in the code.
The two data blocks mentioned at the first post were highlighted as expected.
Would you like a link to my test workbook?
 
Upvote 0
After a little trial and error I have found something odd. When I first run the code it highlights the incorrect matches as it should. If I put the correct data according to sheet 2 in sheet 1 and run the code again its not highlighted - as it should, but for testing purposes if I put an incorrect number in again and run the code it doesn't get highlighted which it should? Almost as if its a one time code!!
 
Upvote 0
Try this version:

Code:
Global Dict1 As Object
Global Dict2 As Object


Sub LoadDictionary(ByRef Dict As Object, ByRef Wks As Worksheet)
    Dim cell    As Range, cx      As Long
    Dim Data()  As Variant, Key     As String, rng     As Range
    Dim RngBeg  As Range, RngEnd  As Range, X       As Long
    
        If Dict Is Nothing Then
            Set Dict = CreateObject("Scripting.Dictionary")
            Dict.CompareMode = vbTextCompare
        Else
            Dict.RemoveAll
        End If
            Set RngBeg = Wks.Range("K2")
            Set RngEnd = Wks.Cells(Rows.Count, RngBeg.Column).End(xlUp)
            If RngEnd.Row < RngBeg.Row Then Exit Sub
            
            cx = Wks.Columns("K:ae").Count - 1
            Set rng = Wks.Range(RngBeg, RngEnd)
            
            For Each cell In rng
                Key = Trim(cell.Value)
                If Key <> "" Then
                    If Not Dict.Exists(Key) Then
                        ReDim Data(0)
                        GoSub UpdateData
                        Dict.Add Key, Data  ' Add the Key and Data to this dictionary.
                    Else
                        ' Return the Data for this Key.
                        Data = Dict(Key)
                        GoSub UpdateData
                        ' Save the updated Data for this Key.
                        Dict(Key) = Data
                    End If
                End If
            Next cell
           
Exit Sub
UpdateData:
            ' Add this cell's row number to the list of rows to highlight for this Key.
            Data(0) = Data(0) & cell.Row & ","
             With cell.Offset(0, cx)
                ' Check if "AD" entry is a duplicate.
                For X = 1 To UBound(Data)
                    If Data(X) = .Value Then Return
                    
                Next X
                ' Save the value in column "AD" in element (1) onward if it is not "NYA" nor "NLA".
                If .Value <> "NYA" And .Value <> "NLA" And .Value <> "NDA" And .Value <> "NA" Then
                    ReDim Preserve Data(UBound(Data) + 1)
                    Data(UBound(Data)) = .Value
                End If
            End With
        Return
End Sub


Sub CatDiffFromSupplier()
    Dim cell        As Range, Cnt         As Long
    Dim cx          As Long, Data1       As Variant, Data2
    Dim j           As Long, k           As Long, Key, n&, nRows
    Dim rng As Range, RngBeg As Range, RngEnd      As Range, Wks As Worksheet
     
        ' Names of the worksheets to compare.
        Call LoadDictionary(Dict1, ActiveWorkbook.Worksheets("Sheet1"))
        Call LoadDictionary(Dict2, ActiveWorkbook.Worksheets("Sheet2"))
        Set Wks = ActiveWorkbook.Worksheets("Sheet1")
        
        cx = Wks.Columns("K:ae").Count
        
        Application.ScreenUpdating = False
            Set RngBeg = Wks.Range("K2")
            Set RngEnd = Wks.Cells(Rows.Count, RngBeg.Column).End(xlUp)
            If RngEnd.Row < RngBeg.Row Then Exit Sub
            Set rng = Wks.Range(RngBeg, RngEnd)
            For Each Key In Dict1.Keys
                Data1 = Dict1(Key)
                Data2 = Dict2(Key)
                Cnt = 1
                
                If VarType(Data2) <> vbEmpty Then
                    ' Check the number of entries is the same.
                    If UBound(Data1) >= UBound(Data2) Then
                        ' Loop through each column "ÄD" entry for Sheet2.
                        For k = 1 To UBound(Data2)
                            ' Check Data1 entries match Data2 entries.
                            For j = 1 To UBound(Data1)
                                If Data1(j) = Data2(k) Then
                                    Cnt = Cnt + 1
                                    Exit For
                                End If
                            Next j
                        Next k
                        ' Was all of Data2 found in Data1?
                        If Cnt < UBound(Data2) + 1 Then GoSub HighlightRows
                        
                    Else
                        GoSub HighlightRows
                    End If
                End If
            Next Key
            
        Application.ScreenUpdating = True
        
Exit Sub
HighlightRows:
            nRows = Split(Data1(0), ",")
            For n = 0 To UBound(nRows) - 1
                Wks.Cells(nRows(n), "A").Resize(1, cx).Interior.ColorIndex = 3
            Next
        Return
End Sub


Sub Reset()
    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Early indications seem to look like it works, thanks. I wil confirm once I give it some more strenuoeus testing.
 
Upvote 0

Forum statistics

Threads
1,223,803
Messages
6,174,689
Members
452,577
Latest member
Filipzgela

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