Split a string using comma and combine withput duplicates

Balajibenz

Board Regular
Joined
Nov 18, 2020
Messages
80
Office Version
  1. 2013
Platform
  1. Windows
Hi People,

Can you help me with below analysis using a VBA code.

I have the sheet with below input in sheet1. It basically has two columns. Column A has ID and B has its category. an ID might have multiple lines and multiple categories each separated by comma. the possible categories are - Production, Development, Testing, UserAcceptanceTest, Staging, DisasterRecovery.

Book1
AB
1IDCategory 1
2100Production, Production, Production
3100Production, Development, Production, Development
4128Production
5115Development, UserAcceptanceTest, UserAcceptanceTest
6115Production, Staging, Staging
7136UserAcceptanceTest, Production, DisasterRecovery
Sheet1


what i am lookin for is to have a single line items for each ID and in the category column all the categories under ID should be refelcting each separated by Comma with out any duplicates in sheet 2 as shown below.

Book1
AB
1IDCategory 2
2100Production, Development, Testing
3128Production
4115Development, UserAcceptanceTest, Production, Staging
5136UserAcceptanceTest, Production, DisasterRecovery
Sheet2


I have few lines but my original data has many and I am unable to attach it due to access issues. also original data will have many duplicates in categroy column (up to 50 each seperated by comma ). Thank you in advance for helping me put.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
How about
VBA Code:
Sub Balajibenz()
   Dim Cl As Range
   Dim Dic As Object
   Dim Sp As Variant
   Dim i As Long
   
   Set Dic = CreateObject("Scripting.dictionary")
   With Sheets("Sheet1")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         Sp = Split(Cl.Offset(, 1).Value, ",")
         For i = 0 To UBound(Sp)
            If Not Dic.exists(Cl.Value) Then Dic.Add Cl.Value, Trim(Sp(i))
            If InStr(1, Dic(Cl.Value), Trim(Sp(i)), 1) = 0 Then
               Dic(Cl.Value) = Dic(Cl.Value) & ", " & Trim(Sp(i))
            End If
         Next i
      Next Cl
   End With
   Sheets("Sheet2").Range("A2").Resize(Dic.Count, 2).Value = Application.Transpose(Array(Dic.keys, Dic.items))
End Sub
 
Upvote 0
How about
VBA Code:
Sub Balajibenz()
   Dim Cl As Range
   Dim Dic As Object
   Dim Sp As Variant
   Dim i As Long
  
   Set Dic = CreateObject("Scripting.dictionary")
   With Sheets("Sheet1")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         Sp = Split(Cl.Offset(, 1).Value, ",")
         For i = 0 To UBound(Sp)
            If Not Dic.exists(Cl.Value) Then Dic.Add Cl.Value, Trim(Sp(i))
            If InStr(1, Dic(Cl.Value), Trim(Sp(i)), 1) = 0 Then
               Dic(Cl.Value) = Dic(Cl.Value) & ", " & Trim(Sp(i))
            End If
         Next i
      Next Cl
   End With
   Sheets("Sheet2").Range("A2").Resize(Dic.Count, 2).Value = Application.Transpose(Array(Dic.keys, Dic.items))
End Sub
Hi Fluff,

Thank you so much, That works perfectly except that if an id has category as empty it is not being published in sheet2. (Sorry that i missed to mention the scenario where category is empty). in that case can you help me to get the ID's to sheet2 with category as empty.

Just one more thing if possible. is it possible produce the result in sheet1 in column "C" without removing duplicates in Column "A". Thanks again mate (y)
 
Upvote 0
This will do the 1st part
VBA Code:
Sub Balajibenz()
   Dim Cl As Range
   Dim Dic As Object
   Dim Sp As Variant
   Dim i As Long
   
   Set Dic = CreateObject("Scripting.dictionary")
   With Sheets("Sheet1")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         If Not Dic.exists(Cl.Value) Then Dic.Add Cl.Value, ""
         Sp = Split(Cl.Offset(, 1).Value, ",")
         For i = 0 To UBound(Sp)
            If Dic(Cl.Value) = "" Then Dic(Cl.Value) = Trim(Sp(i))
            If InStr(1, Dic(Cl.Value), Trim(Sp(i)), 1) = 0 Then
               Dic(Cl.Value) = Dic(Cl.Value) & ", " & Trim(Sp(i))
            End If
         Next i
      Next Cl
   End With
   Sheets("Sheet2").Range("A2").Resize(Dic.Count, 2).Value = Application.Transpose(Array(Dic.keys, Dic.items))
End Sub
 
Upvote 0
Solution
This will do the 1st part
VBA Code:
Sub Balajibenz()
   Dim Cl As Range
   Dim Dic As Object
   Dim Sp As Variant
   Dim i As Long
  
   Set Dic = CreateObject("Scripting.dictionary")
   With Sheets("Sheet1")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         If Not Dic.exists(Cl.Value) Then Dic.Add Cl.Value, ""
         Sp = Split(Cl.Offset(, 1).Value, ",")
         For i = 0 To UBound(Sp)
            If Dic(Cl.Value) = "" Then Dic(Cl.Value) = Trim(Sp(i))
            If InStr(1, Dic(Cl.Value), Trim(Sp(i)), 1) = 0 Then
               Dic(Cl.Value) = Dic(Cl.Value) & ", " & Trim(Sp(i))
            End If
         Next i
      Next Cl
   End With
   Sheets("Sheet2").Range("A2").Resize(Dic.Count, 2).Value = Application.Transpose(Array(Dic.keys, Dic.items))
End Sub
That worked perfectly Fluff. Thank you so much (y)
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
You're welcome & thanks for the feedback.
This will do the 1st part
VBA Code:
Sub Balajibenz()
   Dim Cl As Range
   Dim Dic As Object
   Dim Sp As Variant
   Dim i As Long
  
   Set Dic = CreateObject("Scripting.dictionary")
   With Sheets("Sheet1")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         If Not Dic.exists(Cl.Value) Then Dic.Add Cl.Value, ""
         Sp = Split(Cl.Offset(, 1).Value, ",")
         For i = 0 To UBound(Sp)
            If Dic(Cl.Value) = "" Then Dic(Cl.Value) = Trim(Sp(i))
            If InStr(1, Dic(Cl.Value), Trim(Sp(i)), 1) = 0 Then
               Dic(Cl.Value) = Dic(Cl.Value) & ", " & Trim(Sp(i))
            End If
         Next i
      Next Cl
   End With
   Sheets("Sheet2").Range("A2").Resize(Dic.Count, 2).Value = Application.Transpose(Array(Dic.keys, Dic.items))
End Sub
Hi Fluff,

Can you help me with one more add on to above.in column C I want to categorize based on the categories in B. For example if it is empty then it should be "NO" in column C and if column B has single category then we can have same in column C.
I have few other combinations so can you help me with a template of AND/OR conditions syntax which I can use to fill with all the conditions I have. Thanks again mate.

Something like below.

If column B contains xxx AND xxx AND xxxx then column C. Value = yyyy
Else
column B contains xxx AND xxx OR xxxx then column C. Value = yyyy
 
Upvote 0
You have already started a new thread for this.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

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