Looking for a bit of VBA help (Duplicate finding/ differences).

MiniFav

Board Regular
Joined
Mar 10, 2020
Messages
81
Office Version
  1. 365
Platform
  1. Windows
I am currently trying to create a spreadsheet which will capture various information

Request 1
Column B is for part numbers.
Column C is for serial numbers.
I can have many of the same part numbers but there should never be a duplicate serial number for a single part number.
I may often have the same serial numbers across multiple part numbers though.
I would like these serial number duplicates to be highlighted along with the relevant part number, and if possible return to the default colour when corrected on the page.

Request 2
Column B for the part numbers
Column D for descriptions.
Part numbers should always have the same descriptions. I would like any differences highlighted, and again format returned to default if corrected.

I have this kind of working from various codes from across the internet, however i am finding the loop stops if it encounters a blank cell or that if i correct one description to match another then all the other differences for said part number get cleared too.

OCCMTemplate - test.xlsm
BCD
1PartNo*SerialNo*PartDescription*
2ABC123456Tool
3ABC123322Tool
4ABC123654Tools
5ABC123456Tool
6DEF456322Screw
7DEF456320Screws
8DEF456321Screw
9DEF456321Bolt
10DEF456123Screw
Sheet1




This is the code i have so far (sorry for the state of it).

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

       Dim compRow As Integer, rowNo As Integer

       'Get the last row of data
       lastRow = Worksheets("sheet1").Cells(Rows.Count, 2).End(xlUp).Row

       'Loop through all the rows
      For rowNo = 2 To lastRow

          'For each rowNo, loop through all the remaining rows
          For compRow = rowNo + 1 To lastRow
          

                    
'CORRECTED PN AND SN DUPES'

              'Check if a match is found in Column B for the current rowNo
              If Range("B" & compRow) = Range("B" & rowNo) Then


                  'If a match is found in Column B, check correspoding values of column C
                  If Range("C" & compRow) <> Range("C" & rowNo) Then

                                'Duplicate data accross 3 columns found. Highlight both the rows
                          Range("B" & compRow & ":C" & compRow).Interior.Color = xlNone
                          Range("B" & rowNo & ":C" & rowNo).Interior.Color = xlNone
                          
                    
                          
                          Else
                          
                        If Not Range("B" & compRow & ":B" & compRow).Value = vbNullString And Not Range("B" & compRow & ":B" & compRow).Interior.Color = xlNone Then
                         
                              'Duplicate data accross 3 columns found. Highlight both the rows
                          Range("B" & compRow & ":C" & compRow).Interior.Color = vbYellow
                          Range("B" & rowNo & ":C" & rowNo).Interior.Color = vbYellow
                          
                         
                          End If
                                                 
                    End If
                    
               'End If
                                    

               
'RESTORE CORRECTED TO DEFAULT COLOUR'
               
               'Check if a match is found in Column B for the current rowNo
              If Range("B" & compRow) = Range("B" & rowNo) Then
                        
                        
                            'If a match is found in Columns A and B, check correspoding values of column D
                      If Range("D" & compRow) = Range("D" & rowNo) Then

                          'Duplicate data accross columns found. Highlight both the rows
                          
                          Range("D" & compRow & ":D" & compRow).Interior.Color = xlNone
                          Range("D" & rowNo & ":D" & rowNo).Interior.Color = xlNone
                          
                         Else
                          
                                           
                          'Different data accross columns found. Highlight both the rows
                          Range("D" & compRow & ":D" & compRow).Interior.Color = vbRed
                          Range("D" & rowNo & ":D" & rowNo).Interior.Color = vbRed
                          
                    
                  End If
                          
              End If
              
                          
        End If
                            
                  
          Next compRow
      Next rowNo

End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
One way.
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Rng As Range, R As Range
    Dim SD1 As Object, SD2 As Object
    Dim KeyStr As String, RefStr As String

   'Define range for part number column "B"
    Set Rng = Me.Range("B2:B" & Me.Range("B" & Me.Rows.Count).End(xlUp).Row)

    Set SD1 = CreateObject("Scripting.dictionary")    'for S/N check
    Set SD2 = CreateObject("Scripting.dictionary")    'for desc check

    'RESTORE TO DEFAULT COLOUR'
    Rng.Resize(, 3).Interior.Color = xlNone

    For Each R In Rng
        'HIGHLIGHT PN AND SN DUPES'
        KeyStr = R.Value & ":" & R.Offset(0, 1).Value    'Search Key stored in dictionary
        If Not SD1.Exists(KeyStr) Then                'Unique key value, not already in the dictionary
            SD1.Add KeyStr, vbNullString
        Else
            R.Resize(, 2).Interior.Color = vbYellow   'highlight duplicate P/N + S/N
        End If

        'HIGHLIGHT DIFFENT DESCRIPTIONS FOR SAME PN
        KeyStr = R.Value                              'Search Key stored in dictionary
        If Not SD2.Exists(KeyStr) Then                'Unique key value, not already in the dictionary
            SD2.Add KeyStr, R.Offset(0, 2).Value
        Else
            RefStr = SD2(KeyStr)
            If R.Offset(0, 2).Value <> RefStr Then
                R.Offset(0, 2).Interior.Color = vbRed    'highlight different Desc
            End If
        End If
    Next R
    Set SD1 = Nothing
    Set SD2 = Nothing
End Sub
 
Upvote 0
Wow that was quick, than you for the response.

I have tried your code.

I am currently seeing row 2 is not being picked up as a duplicate.
Also the part description check is not highlighting the test, so I may not know which input is right at this point i would need rows 1,2,5 and 6 highlighted.

OCCMTemplate - test.xlsm
BCD
1PartNo*SerialNo*PartDescription*
2123321test
3123321test
4456654
5123321
6123321
Sheet1


thank you for all your help!
 
Upvote 0
<<at this point i would need rows 1,2,5 and 6 highlighted. >>

Are you saying you want to highlight row 1 (the header row)?
 
Upvote 0
<<at this point i would need rows 1,2,5 and 6 highlighted. >>

Are you saying you want to highlight row 1 (the header row)?
Sorry, I meant 2,3,5 and 6 (any with "123" as the value for example).
 
Upvote 0
This might work more like you want.
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Rng As Range, R As Range
    Dim SD1 As Object, SD2 As Object
    Dim KeyStr As String, RefStr As String
    Dim WS As Worksheet

    Set WS = Me
    Set SD1 = CreateObject("Scripting.dictionary")    'for S/N check
    Set SD2 = CreateObject("Scripting.dictionary")    'for description check

    'Define range for part number column "B"
    Set Rng = WS.Range("B2:B" & WS.Range("B" & WS.Rows.Count).End(xlUp).Row)

    Rng.Resize(, 3).Interior.Color = xlNone

    'Find instances
    For Each R In Rng
        KeyStr = R.Value
        If Not SD1.Exists(KeyStr) Then
            SD1.Add KeyStr, R.Offset(0, 1).Value
        Else
            RefStr = SD1(KeyStr)
            If (RefStr <> R.Offset(0, 1).Value) Then
                SD1(KeyStr) = RefStr & "," & R.Offset(0, 1).Value
            End If
        End If

        KeyStr = R.Value
        If Not SD2.Exists(KeyStr) Then
            SD2.Add KeyStr, R.Offset(0, 2).Value
        Else
            RefStr = SD2(KeyStr)
            If (RefStr <> R.Offset(0, 2).Value) And (Trim(R.Offset(0, 2).Value) <> "") Then
                SD2(KeyStr) = RefStr & "," & R.Offset(0, 2).Value
            End If
        End If
    Next R

    'Highlight cells
    For Each R In Rng

        KeyStr = R.Value
        If SD1.Exists(KeyStr) Then
            RefStr = SD1(KeyStr)
            If UBound(Split(RefStr, ",")) > 0 Then
                R.Resize(, 2).Interior.Color = vbYellow    'highlight P/Ns with the same S/N
            End If
        End If

        If Trim(R.Offset(0, 2).Value) = "" Then
            R.Offset(0, 2).Interior.Color = vbRed     'highlight any missing descriptions
        End If

        KeyStr = R.Value
        If SD2.Exists(KeyStr) Then
            RefStr = SD2(KeyStr)
            If UBound(Split(RefStr, ",")) > 0 Then
                R.Offset(0, 2).Interior.Color = vbRed    'highlight P/Ns with more than one description
            End If
        End If
    Next R

    Set SD1 = Nothing
    Set SD2 = Nothing
End Sub
 
Upvote 0
Than you! this is sooo close.

It seems to be highlighting yellow when the serials are different and clearing when they are duplicate.
I'll try to reverse it and then it will be perfect!

OCCMTemplate - test.xlsm
BCD
1PartNo*SerialNo*PartDescription*
2123A1test
3123A2test
4456654Spoon
5123A2test
6123A4test
7456653Spoons
8789C2
9789C2
10
11789C2
Sheet1


you are a gentleman and a scholar!
 
Upvote 0
Sorry to be a nuisance.

I have reversed the issued i had prior.

I have noticed that if just one serial get changed to be different then all the other duplicates lose their highlight. Could you help with this?

OCCMTemplate - test.xlsm
BCD
1PartNo*SerialNo*PartDescription*
212
312
4
512
611
Sheet1
 
Upvote 0
Actually, when any PN has more than one SN, then all col BC cells for that PN are highlighted. Make sure that all rows with PN ABC have the same SN and the highlighting goes away.
 
Upvote 0

Forum statistics

Threads
1,214,581
Messages
6,120,372
Members
448,957
Latest member
BatCoder

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