VBA Split string from cells into distinct list

rsj88

New Member
Joined
Feb 20, 2018
Messages
38
Hi,

i need the VBA script for the follow is this is possible:

i have the below table in in Sheet 1Columns A-C
1red;blue;greenvan;car;bike
2redcar
3blue;yellowtruck
4blue;green;pinkvan;car;truck;cycle

In Sheet 2 Column A i need a distinct output of column B without the delimiters eg:
Red
blue
yellow
green
pink

In Sheet 2 Column B i need a distinct output of column C without the delimiters eg:
van
car
bike
truck
cycle
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
How about
VBA Code:
Sub rsj()
   Dim Cl As Range
   Dim DicB As Object, DicC As Object
   Dim Itm As Variant
   
   Set DicB = CreateObject("scripting.dictionary")
   Set DicC = CreateObject("scripting.dictionary")
   With Sheets("Sheet1")
      For Each Cl In .Range("B1", .Range("B" & Rows.Count).End(xlUp))
         For Each Itm In Split(Cl.Value, ";")
            DicB(Itm) = Empty
         Next Itm
         For Each Itm In Split(Cl.Offset(, 1).Value, ";")
            DicC(Itm) = Empty
         Next Itm
      Next Cl
   End With
   With Sheets("Sheet2")
      .Range("A2").Resize(DicB.Count).Value = Application.Transpose(DicB.keys)
      .Range("B2").Resize(DicC.Count).Value = Application.Transpose(DicC.keys)
   End With
End Sub
 
Upvote 0
Solution
Method without dictionary

VBA Code:
Sub JEC()
  With Sheets(1)
     ar = Split(Join(Application.Transpose(.Range("A1", .Range("A" & Rows.Count).End(xlUp))), ";"), ";")
     ar2 = Split(Join(Application.Transpose(.Range("B1", .Range("B" & Rows.Count).End(xlUp))), ";"), ";")
  End With
 
  With Sheets(2).Cells(1, 1)
     .Resize(UBound(ar) + 1) = Application.Transpose(ar)
     .Offset(, 1).Resize(UBound(ar2) + 1) = Application.Transpose(ar2)
     .CurrentRegion.Columns(1).RemoveDuplicates 1, 0
     .CurrentRegion.Columns(2).RemoveDuplicates 1, 0
  End With
End Sub
 
Last edited:
Upvote 0
Another one:)

VBA Code:
Sub jec2()
 For Each cl In Sheets(1).Cells(1).CurrentRegion.Cells
   For Each it In Split(cl, ";")
      Select Case cl.Column
          Case 1: If InStr(c00, it) = 0 Then c00 = c00 & it & "|"
          Case 2: If InStr(c01, it) = 0 Then c01 = c01 & it & "|"
      End Select
    Next
 Next
 
 With Sheets(2).Cells(1, 1)
    .Resize(UBound(Split(c00, "|")) + 1) = Application.Transpose(Split(c00, "|"))
    .Offset(, 1).Resize(UBound(Split(c01, "|")) + 1) = Application.Transpose(Split(c01, "|"))
 End With
End Sub
 
Upvote 0
Thanks Fluff. the code is perfect.

JEC your code is prefect too too but only does columns A and B (C and D needed) from the first sheet. however can tweak it and will do the exact same thing.

Thanks both!!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,826
Messages
6,121,797
Members
449,048
Latest member
greyangel23

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