VBA Split semicolon and comma separated values into new rows

Adalinda

New Member
Joined
Jun 18, 2024
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi there,

These are some screenshots of what I'm trying to do:
EXCEL_qNQvD0Embm.png
EXCEL_POGIlcww3u.png


I am trying to split the Alias names into in new rows. There can be 1+ alias names which are separated by semicolon (;). The name format is LAST, FIRST MIDDLE; LAST, FIRST MIDDLE..... Then I need to duplicate the DOB and Additional Columns in each new row. It would be great it I could do all of this in the sheet I'm working in. Right now I am manually insert rows rows and copy/pasting the info and delete from the alias column. I have to do this for about 200 names at a time.

I tried to use the thread info from the post VBA Split comma separated values in new rows and return the original description (VBA Split comma separated values in new rows and return the original description) but I could not get it to work. I am new at macros and would greatly appreciate any help! Thanks :)
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
It seems like the output is inconsistent as to what happens when the 'Alias' column is split.

Here is my attempt in PQ. Let me know if this is what you are looking for.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    tmp = Table.AddColumn(Source, "tmp", each if [Alias] is null then [Alias] else [Last] & " ; " & [Alias]),
    Split = Table.TransformColumns(tmp,{{"tmp", each try Text.Split(_, " ; ") otherwise _}}),
    Expand = Table.ExpandListColumn(Split, "tmp"),
    SplitList = Table.SplitColumn(Expand, "tmp", Splitter.SplitTextByDelimiter(", ", QuoteStyle.Csv), {"tmp.1", "tmp.2"}),
    RC = Table.RemoveColumns(SplitList,{"Last", "Alias"}),
    Rename = Table.RenameColumns(RC,{{"tmp.1", "Last"}, {"tmp.2", "Alias"}}),
    Reorder = Table.ReorderColumns(Rename,{"First", "Middle", "Last", "Alias", "DOB", "Additional"})
in
    Reorder

Book2
ABCDEFGHIJKLM
1FirstMiddleLastAliasDOBAdditionalFirstMiddleLastAliasDOBAdditional
2JenniferDavisMiller, Jennifer ; Wilcox, Jennifer M9/13/2008WASHINGTONJenniferDavis9/13/2008WASHINGTON
3SwiftTony4/11/2015BENTONJenniferMillerJennifer9/13/2008WASHINGTON
4CarmenSheraPlanter10/30/1982PHILLIPSJenniferWilcoxJennifer M9/13/2008WASHINGTON
5CarmenPlanter10/30/1982CONWAYSwift4/11/2015BENTON
6LindaGulleyMccolly, Linda ; Williams, Lenny ; Robertson, Lenny6/25/1991BENTONCarmenShera10/30/1982PHILLIPS
7JeremiahMunroeMonroe, Jeremiah ; Baldrew, Jeremiah5/14/1986LONOKECarmen10/30/1982CONWAY
8JeremiahMunroeMonroe, Jeremiah ; Baldrew, Jeremiah5/14/1986BENTONLindaGulley6/25/1991BENTON
9RobertDoe3/23/2008LONOKELindaMccollyLinda6/25/1991BENTON
10JohnJacobJingle8/22/1996CONWAYLindaWilliamsLenny6/25/1991BENTON
11KevinAnthonyValdez5/19/2002LONOKELindaRobertsonLenny6/25/1991BENTON
12KevinMockingbird5/19/2002PHILLIPSJeremiahMunroe5/14/1986LONOKE
13JeremiahMonroeJeremiah5/14/1986LONOKE
14JeremiahBaldrewJeremiah5/14/1986LONOKE
15JeremiahMunroe5/14/1986BENTON
16JeremiahMonroeJeremiah5/14/1986BENTON
17JeremiahBaldrewJeremiah5/14/1986BENTON
18Robert3/23/2008LONOKE
19JohnJacob8/22/1996CONWAY
20KevinAnthony5/19/2002LONOKE
21Kevin5/19/2002PHILLIPS
Sheet1
 
Upvote 0
I am very new at this. I will have to see if I have Power Query and how to load this into it
 
Upvote 0
@Adalinda
I think I can create a macro to handle this task, but I got a bit confused by the alias part.
Is the second image in post #1 actually the expected result? If not, could you post the correct one?
Also, please provide your example and the result in text format instead of an image.
To post your example, you could use the XL2BB tool or at least just copy your data range and paste it here.
 
Upvote 0
@Adalinda
I think I can create a macro to handle this task, but I got a bit confused by the alias part.
Is the second image in post #1 actually the expected result? If not, could you post the correct one?
Also, please provide your example and the result in text format instead of an image.
To post your example, you could use the XL2BB tool or at least just copy your data range and paste it here.
I get an excel that looks like Input. There a list of names (column A-C) but some of them also have Alias names. But they put them in Column D in this format {Last Name}, {First Name} {Middle Name(s)}. They sometimes have one Alias Name but more often there are 2-5 and sometimes they have multiple middle names. I will go in manually and insert a row below the "Main Name" and copy/paste the Alias name(s) into their proper columns (A-C). Once I have added all the Alias names under the "Main Name" I then copy columns E-F and paste them into the new Alias name rows. The end product I get looks like the Output image

Macro Play.xlsx
ABCDEFGHIJKLM
1InputOutput
2First NameMiddle NameLast NameAliasDOBAdditionalFirst NameMiddle NameLast NameAliasDOBAdditional
3JenniferDavisMiller, Jennifer ; Wilcox, Jennifer M9/13/2008WASHINGTONJenniferDavis9/13/2008WASHINGTON
4SwiftTony4/11/2015BENTONJenniferMiller9/13/2008WASHINGTON
5CarmenSheraPlanter10/30/1982PHILLIPSJenniferMWilcox9/13/2008WASHINGTON
6CarmenPlanter10/30/1982CONWAYSwiftTony4/11/2015BENTON
7LindaGulleyMccolly, Linda ; Williams, Lenny ; Robertson, Lenny6/25/1991BENTONCarmenSheraPlanter10/30/1982PHILLIPS
8JeremiahMunroeMonroe, Jeremiah ; Baldrew, Jeremiah5/14/1987LONOKECarmenPlanter10/30/1982CONWAY
9JeremiahMunroeMonroe, Jeremiah ; Baldrew, Jeremiah5/14/1986BENTONLindaGulley6/25/1991BENTON
10RobertDoe3/23/2008LONOKELindaMccolly6/25/1991BENTON
11JohnJacobJingle8/22/1996CONWAYLennyWilliams6/25/1991BENTON
12KevinAnthonyValdez5/19/2002LONOKELennyRobertson6/25/1991BENTON
13KevinMockingbird5/19/2002PHILLIPJeremiahMunroe5/14/1987LONOKE
14JeremiahMonroe5/14/1987LONOKE
15JeremiahBaldrew5/14/1987LONOKE
16JeremiahMunroe5/14/1986BENTON
17JeremiahMonroe5/14/1986BENTON
18JeremiahBaldrew5/14/1986BENTON
19RobertDoe3/23/2008LONOKE
20JohnJacobJingle8/22/1996CONWAY
21KevinAnthonyValdez5/19/2002LONOKE
22KevinMockingbird5/19/2002PHILLIP
Sheet1
 
Upvote 0
First, delete 1st row that you have "Input" & "Output" then run the code
Code:
Sub test()
    Dim a, b, e, i&, ii&, n&, x
    a = [a1].CurrentRegion.Value
    ReDim b(1 To UBound(a, 1) * 10, 1 To UBound(a, 2))
    For i = 2 To UBound(a, 1)
        n = n + 1: x = Split(a(i, 4), ";"): a(i, 4) = ""
        For ii = 1 To UBound(a, 2)
            b(n, ii) = a(i, ii)
        Next
        If UBound(x) > -1 Then
            For Each e In x
                n = n + 1
                For ii = 1 To UBound(a, 2)
                    b(n, ii) = a(i, ii)
                Next
                b(n, 1) = Trim$(Split(e & ",", ",")(0))
                b(n, 2) = Trim$(Split(e & ",", ",")(1))
            Next
        End If
    Next
    With [h1].Resize(, UBound(b, 2))
        .CurrentRegion.ClearContents
        .Value = a
        .Rows(2).Resize(n).Value = b
    End With
End Sub
 
Upvote 0
Power Query is a free AddIn for Excel 2010 and 2013, and is built-in functionality from Excel 2016 onwards (where it is referred to as "Get & Transform Data").

It is a powerful yet simple way of getting, changing and using data from a broad variety of sources, creating steps which may be easily repeated and refreshed. I strongly recommend learning how to use Power Query - it's among the most powerful functionalities of Excel.

- Follow this link to learn how to install Power Query in Excel 2010 / 2013.

- Follow this link for an introduction to Power Query functionality.

- Follow this link for a video which demonstrates how to use Power Query code provided.
 
Upvote 0
No one was even going to respond and I'm just getting ignored. Cool. At least Alan gives a shout out to PQ.
 
Upvote 0
Power Query is a free AddIn for Excel 2010 and 2013, and is built-in functionality from Excel 2016 onwards (where it is referred to as "Get & Transform Data").

It is a powerful yet simple way of getting, changing and using data from a broad variety of sources, creating steps which may be easily repeated and refreshed. I strongly recommend learning how to use Power Query - it's among the most powerful functionalities of Excel.

- Follow this link to learn how to install Power Query in Excel 2010 / 2013.

- Follow this link for an introduction to Power Query functionality.

- Follow this link for a video which demonstrates how to use Power Query code provided.
You are amazing! Thank you for this info. I am trying to learn how to do macros and more data manipulation but its all self teaching. LOL
 
Upvote 0
No one was even going to respond and I'm just getting ignored. Cool. At least Alan gives a shout out to PQ.
Sorry I thought I did respond. I just put it as a new comment not a reply. I have been trying to figure out how to test out your code since I didn't know what PQ was. With Alan's info/suggestions I can hopefully figure it out. Again thank you for your help. I really appreciate it.
 
Upvote 0

Forum statistics

Threads
1,223,877
Messages
6,175,138
Members
452,614
Latest member
MRSWIN2709

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