Parse String to adjacent Columns

airforceone

Board Regular
Joined
Feb 14, 2022
Messages
201
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Good day mate, need some guidance here
1. got a code with some loop error and
2. would like to parse the cell value into adjacent column

attached here is the source, Code Result and Expected Result (Hopefully)

source

Michael Smith (49/Male/Hospitalized/Alien/DRIVER)
James Smith (18/Male/Hospitalized/Alien/JOBLESS)
Robert Smith (18/Male/Hospitalized/Alien/STUDENT)
Maria Garcia (43/Female/Hospitalized/Alien/FARMER)
David Smith (15/Female/Deceased/Alien/STUDENT)
Maria Rodriguez (46/Female/Hospitalized/Alien/FARMER)
Mary Smith (2/Male/Hospitalized/Alien/JOBLESS)
Gerald Golbuno (22/Male/Hospitalized/Alien/STUDENT)
Maria Hernandez (34/Female/Hospitalized/Alien/JOBLESS)
Maria Martinez (37/Female/Hospitalized/Alien/JOBLESS)
James Johnson (40/Male/Hospitalized/Alien/JOBLESS)
Maria Michael (37/Female/Hospitalized/Alien/JOBLESS)
James Biggins (38/Male/Hospitalized/Alien/JOBLESS)


Code Result (With some kind of Loop error, when run)

Michael Smith (49/Male/Hospitalized/Alien/DRIVER)MichaelSmith(49MaleHospitalizedAlienDRIVER)
James Smith (18/Male/Hospitalized/Alien/JOBLESS)JamesSmith(18MaleHospitalizedAlienJOBLESS)
Robert Smith (18/Male/Hospitalized/Alien/STUDENT)RobertSmith(18MaleHospitalizedAlienSTUDENT)
Maria Garcia (43/Female/Hospitalized/Alien/FARMER)MariaGarcia(43FemaleHospitalizedAlienFARMER)
David Smith (15/Female/Deceased/Alien/STUDENT)DavidSmith(15FemaleDeceasedAlienSTUDENT)
Maria Rodriguez (46/Female/Hospitalized/Alien/FARMER)MariaRodriguez(46FemaleHospitalizedAlienFARMER)
Mary Smith (2/Male/Hospitalized/Alien/JOBLESS)MarySmith(2MaleHospitalizedAlienJOBLESS)
Gerald Golbuno (22/Male/Hospitalized/Alien/STUDENT)GeraldGolbuno(22MaleHospitalizedAlienSTUDENT)
Maria Hernandez (34/Female/Hospitalized/Alien/JOBLESS)MariaHernandez(34FemaleHospitalizedAlienJOBLESS)
Maria Martinez (37/Female/Hospitalized/Alien/JOBLESS)MariaMartinez(37FemaleHospitalizedAlienJOBLESS)
James Johnson (40/Male/Hospitalized/Alien/JOBLESS)JamesJohnson(40MaleHospitalizedAlienJOBLESS)
Maria Michael (37/Female/Hospitalized/Alien/JOBLESS)MariaMichael(37FemaleHospitalizedAlienJOBLESS)
James Biggins (38/Male/Hospitalized/Alien/JOBLESS)JamesBiggins(38MaleHospitalizedAlienJOBLESS)
CODE RESULT


Expected Result

Michael Smith (49/Male/Hospitalized/Alien/DRIVER)Michael Smith49MaleHospitalizedAlienDRIVER
James Smith (18/Male/Hospitalized/Alien/JOBLESS)James Smith18MaleHospitalizedAlienJOBLESS
Robert Smith (18/Male/Hospitalized/Alien/STUDENT)Robert Smith18MaleHospitalizedAlienSTUDENT
Maria Garcia (43/Female/Hospitalized/Alien/FARMER)Maria Garcia43FemaleHospitalizedAlienFARMER
David Smith (15/Female/Deceased/Alien/STUDENT)David Smith15FemaleDeceasedAlienSTUDENT
Maria Rodriguez (46/Female/Hospitalized/Alien/FARMER)Maria Rodriguez46FemaleHospitalizedAlienFARMER
Mary Smith (2/Male/Hospitalized/Alien/JOBLESS)Mary Smith2MaleHospitalizedAlienJOBLESS
Gerald Golbuno (22/Male/Hospitalized/Alien/STUDENT)Gerald Golbuno22MaleHospitalizedAlienSTUDENT
Maria Hernandez (34/Female/Hospitalized/Alien/JOBLESS)Maria Hernandez34FemaleHospitalizedAlienJOBLESS
Maria Martinez (37/Female/Hospitalized/Alien/JOBLESS)Maria Martinez37FemaleHospitalizedAlienJOBLESS
James Johnson (40/Male/Hospitalized/Alien/JOBLESS)James Johnson40MaleHospitalizedAlienJOBLESS
Maria Michael (37/Female/Hospitalized/Alien/JOBLESS)Maria Michael37FemaleHospitalizedAlienJOBLESS
James Biggins (38/Male/Hospitalized/Alien/JOBLESS)James Biggins38MaleHospitalizedAlienJOBLESS
EXPECTED RESULT




VBA Code:
Private Sub Parse_String()

    Dim rngSource As Range
    Dim LastRow As Long, iLoop As Long
    Dim LastColHeader As String
    
    LastRow = Sheets("SOURCE").UsedRange.Rows.Count

    With Sheets("SOURCE")
        Set rngSource = Sheets("SOURCE").Range("A1:A" & LastRow)
    End With

    Dim Targetcel As Range
    For Each Targetcel In rngSource
        Targetcel.Value = WorksheetFunction.Substitute(Targetcel.Value, "-", ", ")
    Next Targetcel

    For iLoop = 1 To LastRow
        LastColHeader = Split(Cells(iLoop, 2).Address, "$")(1)
        rngSource.TextToColumns _
                Destination:=Range(LastColHeader & iLoop & ":" & LastColHeader & iLoop & ":" & LastColHeader & iLoop & ":" & LastColHeader & iLoop & ":" & LastColHeader & iLoop), _
                DataType:=xlDelimited, _
                Tab:=False, _
                Semicolon:=False, _
                Comma:=False, _
                Space:=True, _
                Other:=True, _
                OtherChar:="/"
    Next iLoop
    
End Sub     ' Parse_String
 
Not sure what you are doing with the results (if anything) or whether it matters to you but with your sample data, the code leaves all the values in the name column with a trailing space.

If interested, here is another option that addresses that issue and only requires a single text to columns.

VBA Code:
Sub TTC()
  With Range("B1:B" & Range("A" & Rows.Count).End(xlUp).Row)
    .Value = Application.Substitute(Application.Substitute(.Offset(, -1).Value, " (", "/"), ")", "")
    .TextToColumns , 1, , , 0, 0, 0, 0, 1, "/"
  End With
End Sub
yes did see it (trailing space) after the name and the code you've given seems to be alot more faster!
but given that I'm new at excel vba the first code seems more readable and and alot easier to understand :)
 
Upvote 0

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
My code transfers col A to col B & at the same time replaces the " (" with a "/" and removes the final ")".
Then it just does a text to columns on col B with "/" as the delimiter.

Still, you are right to go with the code that you are most comfortable with. :)

Thanks for the follow-up though. (y)
 
Upvote 0
My code transfers col A to col B & at the same time replaces the " (" with a "/" and removes the final ")".
Then it just does a text to columns on col B with "/" as the delimiter.

Still, you are right to go with the code that you are most comfortable with. :)

Thanks for the follow-up though. (y)
no problem mate
much appreciated someone actually checking on someone's submitted post/answer though working, might have something more faster and less code to issue!
cheers mate till my/our next query :)
 
Upvote 0

Forum statistics

Threads
1,223,880
Messages
6,175,153
Members
452,615
Latest member
bogeys2birdies

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