excelvbatext-to-column

Manipulate text to column function in Excel and separate text with variable number of delimiters


I download data from a vendor and the identifying data, (the first column), has an individual's name and their participant number in the same column separated by commas.

The info is not in general format for Excel, the cells are in text format. I will convert that afterward.

The number of commas in the identifying data column is not the same.

Participant number will always be last in the identifying column. If an individual wants to be anonymous the name will not be there or partially there.

I am trying to separate it so:

The names will change and the number of lines will change frequently but probably somewhere about 1000 rows every time I run this macro.

Data starts out like this:
Before using my macro

I made this by using record macro in Excel.
I add two columns to the right of the identifying column then do a text to column feature to separate the data by commas. The identifying data is put into three columns and I rename the three columns.

Sub Add_and_rename_columns()

' Add_and_rename_columns Macro

'
    Columns("B:B").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Columns("A:A").Select
    Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
      TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
      Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
      :=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
    Range("A3").Select
    ActiveCell.FormulaR1C1 = "Participant Last Name"
    Range("B3").Select
    ActiveCell.FormulaR1C1 = "Participant First Name"
    Range("C3").Select
    ActiveCell.FormulaR1C1 = "Participant Number"
    
End Sub

Afterwards I get:
Post macro

Not all of the participant numbers are in the same column.


Solution

  • If I understand you correctly ....

    Data before running the sub is something like this :
    enter image description here

    After running the sub (expected result) :
    enter image description here

    Sub test()
    Dim rg As Range: Dim cell As Range
    Dim cnt As Integer
    
    With ActiveSheet
    Set rg = .Range("A4", .Range("A" & Rows.Count).End(xlUp))
    End With
    
    rg(1, 2).Resize(1, 2).EntireColumn.Insert
    
    For Each cell In rg
        cnt = Len(cell.Value) - Len(Replace(cell.Value, ",", ""))
        With cell.Resize(1, 3)
            If cnt = 0 Then .Value = Split(",," & cell.Value, ",")
            If cnt = 1 Then .Value = Split(Replace(cell.Value, ",", ",,"), ",")
            If cnt = 2 Then .Value = Split(cell.Value, ",")
        End With
    Next
    
    Range("A3").Value = "Participant Last Name"
    Range("B3").Value = "Participant First Name"
    Range("C3").Value = "Participant Number"
    
    End Sub
    

    rg variable is the range of data in column A starting from row 4 to the last row with data, then it insert two columns and then loop to each cell in rg.

    within the loop, it fill each looped cell.resize(1,3) with the array coming from splitting the cell value by comma separated. It check first if there is no comma in the looped cell value, then it add two commas in front of that value then split it. If there is one comma in the looped cell value, then it replace that one comma with two commas, then split it. If there is two commas in the looped cell value, then it just split the looped cell value.