VBA: adding delimited string to a dynamic array

jscranton

Well-known Member
Joined
May 30, 2011
Messages
707
Working on a new project and thinking about the best way to achieve something.

I have a range of cell that each contain a string of names delimited by semi-colons such as "Last, First;Last2, First2;Last3, First3". In the end, I want each name String to be added to an array (I will dedupe the array once all values have been added).

I have used the split function before and I think perhaps I could use the copy array function to add them to the destination array? So, it would look something like:

Code:
 Dim nameString As String
 Dim tempArray() As String


For i = 2 to LastRowOfRange

   nameString = CSTR(Range("A"& i).Value)
   tempArray  = SPLIT(nameString,";")

   P[COLOR=#003366]ublic Function CopyArray(DestinationArray As Variant, tempArray As Variant, _
        Optional NoCompatabilityCheck As Boolean = False) As Boolean[/COLOR]

Next i


End Sub

Assuming this works, is there an easier ("cleaner") way to achieve this?
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
jscranton,

Sample raw data:


Excel 2007
ABC
1Title A
2Last, First;Last2, First2;Last3, First3
3Last2, First2;Last3, First3;Last, First
4Last5, First5;Last6, First6;Last8, First8
5Last, First;Last2, First2;Last3, First3
6Last2, First2;Last3, First3;Last, First
7Last7, First7;Last6, First6;Last8, First8
8
9
Sheet1


After the macro:


Excel 2007
ABC
1Title AUniques
2Last, First;Last2, First2;Last3, First3Last, First
3Last2, First2;Last3, First3;Last, FirstLast2, First2
4Last5, First5;Last6, First6;Last8, First8Last3, First3
5Last, First;Last2, First2;Last3, First3Last5, First5
6Last2, First2;Last3, First3;Last, FirstLast6, First6
7Last7, First7;Last6, First6;Last8, First8Last7, First7
8Last8, First8
9
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Option Explicit
Sub GetUniques()
' hiker95, 02/01/2014, ME754614
Dim d As Object, a As Variant, i As Long, s, ii As Long
Set d = CreateObject("Scripting.Dictionary")
a = Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row)
For i = LBound(a, 1) To UBound(a, 1)
  If InStr(a(i, 1), ";") = 0 And a(i, 1) <> "" Then
    If Not d.Exists(a(i, 1)) Then
      d(a(i, 1)) = 1
    End If
  ElseIf InStr(a(i, 1), ";") > 0 Then
    s = Split(a(i, 1), ";")
    For ii = LBound(s) To UBound(s)
      If Not d.Exists(s(ii)) Then
        d(s(ii)) = 1
      End If
    Next ii
  End If
Next i
Columns(3).Clear
Cells(1, 3) = "Uniques"
Range("C2").Resize(d.Count) = Application.Transpose(d.Keys)
Range("C2:C" & d.Count + 1).Sort key1:=Range("C2"), order1:=1
Columns(3).AutoFit
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the GetUniques macro.
 
Upvote 0
jscranton,

Sample raw data:

Excel 2007
ABC
Last, First;Last2, First2;Last3, First3
Last2, First2;Last3, First3;Last, First
Last5, First5;Last6, First6;Last8, First8
Last, First;Last2, First2;Last3, First3
Last2, First2;Last3, First3;Last, First
Last7, First7;Last6, First6;Last8, First8

<tbody>
[TD="align: center"]1[/TD]
[TD="align: center"]Title A[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]2[/TD]

[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]3[/TD]

[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]4[/TD]

[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]5[/TD]

[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]6[/TD]

[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]7[/TD]

[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]8[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]9[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

</tbody>
Sheet1
Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Option Explicit
Sub GetUniques()
' hiker95, 02/01/2014, ME754614
Dim d As Object, a As Variant, i As Long, s, ii As Long
Set d = CreateObject("Scripting.Dictionary")
a = Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row)
For i = LBound(a, 1) To UBound(a, 1)
  If InStr(a(i, 1), ";") = 0 And a(i, 1) <> "" Then
    If Not d.Exists(a(i, 1)) Then
      d(a(i, 1)) = 1
    End If
  ElseIf InStr(a(i, 1), ";") > 0 Then
    s = Split(a(i, 1), ";")
    For ii = LBound(s) To UBound(s)
      If Not d.Exists(s(ii)) Then
        d(s(ii)) = 1
      End If
    Next ii
  End If
Next i
Columns(3).Clear
Cells(1, 3) = "Uniques"
Range("C2").Resize(d.Count) = Application.Transpose(d.Keys)
Range("C2:C" & d.Count + 1).Sort key1:=Range("C2"), order1:=1
Columns(3).AutoFit
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the GetUniques macro.
IF the OP will always be using XL2007 or newer, then we can remove the loops and Dictionary object from your code and use some built-in functionality offered by these versions... should speed things up some as well I would imagine.
Code:
Sub GetUniques()
  Dim AllNames As String, IndividualNames() As String
  AllNames = Join(Application.Transpose(Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row).Value), ";")
  IndividualNames = Split(AllNames, ";")
  Columns("C").Clear
  Range("C1").Value = "Uniques"
  With Range("C2:C" & UBound(IndividualNames) + 1)
    .Value = Application.Transpose(IndividualNames)
    .RemoveDuplicates Columns:=1, Header:=xlNo
    .Sort key1:=Range("C2"), Order1:=1
  End With
  Columns("C").AutoFit
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,928
Members
452,366
Latest member
TePunaBloke

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