Duplicate record handling?

TheWennerWoman

Active Member
Joined
Aug 1, 2019
Messages
313
Office Version
  1. 365
Platform
  1. Windows
I have some data with duplicate records that I'd like to be able to loop through in VBA and make an amendment to. So
Code:
ABC
ABD
ABD
ABE
ABG
ABG
ABG

In the above data, I'd want the first ABD to be renamed ABD* and the second ABD to be renamed ABD**.

Like wise the three ABG records would become ABG*, ABG** and ABG***.

Is this do-able?

Many thanks as always.
 
Since you did mention VBA and your previous posts seem to revolve aroundVBA, here is another option.
(Assumes data starts at row one and output is to Column B)

VBA Code:
Sub AddWildCardToDups()

    Dim shtData As Worksheet
    Dim rngData As Range, arrData As Variant
    Dim lrData As Long, i As Long
    Dim dictData As Object, dictKey As String

    Set dictData = CreateObject("Scripting.dictionary")
    
    Set shtData = ActiveSheet               ' <--- Change as required
    With shtData
        lrData = .Range("A" & Rows.Count).End(xlUp).Row
        Set rngData = .Range(.Cells(1, "A"), .Cells(lrData, "A"))
        arrData = rngData.Value
        ReDim Preserve arrData(1 To UBound(arrData), 1 To 2)
    End With
    
    For i = 1 To UBound(arrData)
        dictKey = arrData(i, 1)
        If Not dictData.exists(dictKey) Then
            dictData(dictKey) = 1
            arrData(i, 2) = arrData(i, 1) & String(dictData(dictKey), "*")
        Else
            dictData(dictKey) = dictData(dictKey) + 1
              arrData(i, 2) = arrData(i, 1) & String(dictData(dictKey), "*")
        End If
    Next i
    
    ' Write back updated data
    rngData.Offset(, 1).Value = Application.Index(arrData, 0, 2)

End Sub
 
Upvote 0
Since you did mention VBA and your previous posts seem to revolve aroundVBA, here is another option.
(Assumes data starts at row one and output is to Column B)

VBA Code:
Sub AddWildCardToDups()

    Dim shtData As Worksheet
    Dim rngData As Range, arrData As Variant
    Dim lrData As Long, i As Long
    Dim dictData As Object, dictKey As String

    Set dictData = CreateObject("Scripting.dictionary")
  
    Set shtData = ActiveSheet               ' <--- Change as required
    With shtData
        lrData = .Range("A" & Rows.Count).End(xlUp).Row
        Set rngData = .Range(.Cells(1, "A"), .Cells(lrData, "A"))
        arrData = rngData.Value
        ReDim Preserve arrData(1 To UBound(arrData), 1 To 2)
    End With
  
    For i = 1 To UBound(arrData)
        dictKey = arrData(i, 1)
        If Not dictData.exists(dictKey) Then
            dictData(dictKey) = 1
            arrData(i, 2) = arrData(i, 1) & String(dictData(dictKey), "*")
        Else
            dictData(dictKey) = dictData(dictKey) + 1
              arrData(i, 2) = arrData(i, 1) & String(dictData(dictKey), "*")
        End If
    Next i
  
    ' Write back updated data
    rngData.Offset(, 1).Value = Application.Index(arrData, 0, 2)

End Sub

That mostly works - many thanks and thanks to the other responders.

If there is only one occurrence, just leave it alone. So ABC in my sample data would stay as ABC, only duplicate records need to have a * appended.
 
Upvote 0
I was waiting for that.

Try this:
VBA Code:
Sub AddWildCardToDups_v02()

    Dim shtData As Worksheet
    Dim rngData As Range, arrData As Variant
    Dim lrData As Long, i As Long
    Dim dictData As Object, dictKey As String, firstID As Long

    Set dictData = CreateObject("Scripting.dictionary")
   
    Set shtData = ActiveSheet               ' <--- Change as required
    With shtData
        lrData = .Range("A" & Rows.Count).End(xlUp).Row
        Set rngData = .Range(.Cells(1, "A"), .Cells(lrData, "A"))
        arrData = rngData.Value
        ReDim Preserve arrData(1 To UBound(arrData), 1 To 2)
    End With
   
    ' Load details range into Dictionary
    For i = 1 To UBound(arrData)
        dictKey = arrData(i, 1)
        If Not dictData.exists(dictKey) Then
            firstID = -1 * i                    ' Store row ref as negative
            dictData(dictKey) = firstID
            arrData(i, 2) = arrData(i, 1)
        ElseIf dictData(dictKey) < 0 Then
            ' retrieve first occurence and add "*"
            firstID = Abs(dictData(dictKey))
            arrData(firstID, 2) = arrData(firstID, 1) & "*"
            ' 2nd occurence
            dictData(dictKey) = 2
            arrData(i, 2) = arrData(i, 1) & String(dictData(dictKey), "*")
        Else
            dictData(dictKey) = dictData(dictKey) + 1
            arrData(i, 2) = arrData(i, 1) & String(dictData(dictKey), "*")
        End If
    Next i
   
    ' Write back updated data
    rngData.Offset(, 1).Value = Application.Index(arrData, 0, 2)

End Sub
 
Upvote 1
Solution
I was waiting for that.

Try this:
VBA Code:
Sub AddWildCardToDups_v02()

    Dim shtData As Worksheet
    Dim rngData As Range, arrData As Variant
    Dim lrData As Long, i As Long
    Dim dictData As Object, dictKey As String, firstID As Long

    Set dictData = CreateObject("Scripting.dictionary")
  
    Set shtData = ActiveSheet               ' <--- Change as required
    With shtData
        lrData = .Range("A" & Rows.Count).End(xlUp).Row
        Set rngData = .Range(.Cells(1, "A"), .Cells(lrData, "A"))
        arrData = rngData.Value
        ReDim Preserve arrData(1 To UBound(arrData), 1 To 2)
    End With
  
    ' Load details range into Dictionary
    For i = 1 To UBound(arrData)
        dictKey = arrData(i, 1)
        If Not dictData.exists(dictKey) Then
            firstID = -1 * i                    ' Store row ref as negative
            dictData(dictKey) = firstID
            arrData(i, 2) = arrData(i, 1)
        ElseIf dictData(dictKey) < 0 Then
            ' retrieve first occurence and add "*"
            firstID = Abs(dictData(dictKey))
            arrData(firstID, 2) = arrData(firstID, 1) & "*"
            ' 2nd occurence
            dictData(dictKey) = 2
            arrData(i, 2) = arrData(i, 1) & String(dictData(dictKey), "*")
        Else
            dictData(dictKey) = dictData(dictKey) + 1
            arrData(i, 2) = arrData(i, 1) & String(dictData(dictKey), "*")
        End If
    Next i
  
    ' Write back updated data
    rngData.Offset(, 1).Value = Application.Index(arrData, 0, 2)

End Sub
Genius, thank you so much, works perfectly
 
Upvote 0

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