VBA - Count duplicates with unique values in a column with delimiters and display results

datdog22

New Member
Joined
May 23, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I'm trying to modify this VBA Code I found at another site to be able to count duplicates with unique values that sometimes have delimiters. I messaged the site owner and haven't received a response so I am asking here for your help.
I have data in a column and sometimes the data has delimiters (always a forward slash). For example:
AA
CC
BB
AA/CC
AA/BB/CC
I would like to display the number of each unique value found. So in my example the result would be:
AA - 3
BB - 2
CC - 3

VBA Code:
Sub Count_Duplicates_With_Unique_Values()

Set Rng = Range("B3:B13")

Count = 0
Count2 = 0
Output = ""
Match = False

Dim Elements() As Variant
ReDim Elements(0)
Elements(0) = "ExcelDemy"

For i = 1 To Rng.Rows.Count
    For j = LBound(Elements) To UBound(Elements)
        If Elements(j) = Rng.Cells(i, 1).Value Then

            Match = True
            Exit For
        End If
    Next j
    If Match = False Then
        Count2 = Count2 + 1
        ReDim Preserve Elements(Count2)
        Elements(Count2) = Rng.Cells(i, 1).Value
        For j = 1 To Rng.Rows.Count
            If Rng.Cells(j, 1) = Rng.Cells(i, 1) Then
                Count = Count + 1
            End If
        Next j
        Output = Output + vbNewLine + Rng.Cells(i, 1) + ": " + Str(Count)
        Count = 0
    End If
    Match = False
Next i

MsgBox Output

End Sub

The code works great but it just doesn't work with delimiters. Thank you for your help.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
VBA Code:
Sub Count_Unique()
   
     Set dict = CreateObject("scripting.dictionary")
     dict.comparemode = vbTextCompare 'not case sensitive
     
     Set c = Range("B3:B13")
     For Each cl In c.Cells
          sp = Split(cl.Value, "/") 'split on "/"
          For i = 0 To UBound(sp) 'each part
               dict(sp(i)) = dict(sp(i)) + 1
          Next
     Next
     
     With Range("D1").Resize(dict.Count)
          .Value = Application.Transpose(dict.keys)
          .Offset(, 1).Value = Application.Transpose(dict.items)
     End With

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,940
Messages
6,122,356
Members
449,080
Latest member
Armadillos

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