Text to column but in rows

Gehree

New Member
Joined
Jul 16, 2018
Messages
7
Hi All,

First time on this forum and was hoping to get some help. I am trying to do a text to column but in vertical format instead of having it split horizontally.

For example,
My data now looks something like this.
NameAttributes
TomMale, Teen, Tall, Glasses, Black hair
HenryMale, Adult, Short
ClarissaFemale, Adult
SamMale, Child, Short, Black hair
SarahFemale, Teen, Red hair

<tbody>
</tbody>

I require it to look something like this.
NameAttributes
TomMale
Tom
Teen
TomTall
TomGlasses
TomBlack hair
HenryMale
HenryAdult
HenryShort
ClarissaFemale
ClarissaAdult
SamMale
SamChild
SamShort
SamBlack hair
SarahFemale
SarahTeen
SarahRed hair

<tbody>
</tbody>

Any help would be greatly appreciated!

Thank you!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Gehree,

Welcome to the Board.

If you're comfortable with a vba approach, you might consider the following...

Code:
Sub TextToRows()
Dim i As Long
Dim arr As Variant

For i = Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1
    arr = Split(Cells(i, 2).Value, ",")
    Rows(i + 1 & ":" & i + UBound(arr)).Insert
    Cells(i, 1).Resize(UBound(arr) + 1, 1) = Cells(i, 1).Value
    Cells(i, 2).Resize(UBound(arr) + 1, 1) = Application.Transpose(arr)
Next i
End Sub

Cheers,

tonyyy
 
Last edited:
Upvote 0
Gehree,

Welcome to the Board.

If you're comfortable with a vba approach, you might consider the following...

Code:
Sub TextToRows()
Dim i As Long
Dim arr As Variant

For i = Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1
    arr = Split(Cells(i, 2).Value, ",")
    Rows(i + 1 & ":" & i + UBound(arr)).Insert
    Cells(i, 1).Resize(UBound(arr) + 1, 1) = Cells(i, 1).Value
    Cells(i, 2).Resize(UBound(arr) + 1, 1) = Application.Transpose(arr)
Next i
End Sub

Cheers,

tonyyy

Hi Tony,

I'm not too familiar with VBA but when I copied this into the macro box and tried to run it, It says there's a run time error '1004'.

I can't recognize the scripting that you did so I don't know what went wrong. Is there any additional steps that I have to take?

Thank you!

Regards,
Gehree
 
Upvote 0
The code from tonyyy ran fine for me, do you have any protection on the sheet and is your data in columns A & B?

Also is the sheet you want the macro on the activesheet? and also describe in more detail what you done to run the code.
 
Last edited:
Upvote 0
The code from tonyyy ran fine for me, do you have any protection on the sheet and is your data in columns A & B?

Also is the sheet you want the macro on the activesheet? and also describe in more detail what you done to run the code.

Hi Mark,

I don't have any protection and yes my data is in columns A and B.

I basically copied the whole text, went under "Developer" tab, selected "Visual Basic" icon and pasted the coding into the empty space. I then closed it and clicked on "Macros" icon and clicked "Run".

The 7th line in his code, "Cells(i, 1).Resize(UBound(arr) + 1, 1) = Cells(i, 1).Value" has been highlighted yellow, I assume that means the issue lies in this line for me?

Sorry, I'm very bad at this.

Regards,
Gehree
 
Upvote 0
Thanks, Mark858, for stepping in.

Gehree - The error could be generated if there's an empty cell in Column B. (An empty cell would not create an array, and therefore Ubound(arr) would fail.) You might try the following modification to skip past empty cells.

Code:
Sub TextToRows()
Application.ScreenUpdating = False
Dim i As Long
Dim arr As Variant

For i = Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1
    If Cells(i, 2) <> "" Then
        arr = Split(Cells(i, 2).Value, ",")
        Rows(i + 1 & ":" & i + UBound(arr)).Insert
        Cells(i, 1).Resize(UBound(arr) + 1, 1) = Cells(i, 1).Value
        Cells(i, 2).Resize(UBound(arr) + 1, 1) = Application.Transpose(arr)
    End If
Next i
End Sub
 
Last edited:
Upvote 0
Thanks, Mark858, for stepping in.

Gehree - The error could be generated if there's an empty cell in Column B. (An empty cell would not create an array, and therefore Ubound(arr) would fail.) You might try the following modification to skip past empty cells.

Code:
Sub TextToRows()
Application.ScreenUpdating = False
Dim i As Long
Dim arr As Variant

For i = Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1
    If Cells(i, 2) <> "" Then
        arr = Split(Cells(i, 2).Value, ",")
        Rows(i + 1 & ":" & i + UBound(arr)).Insert
        Cells(i, 1).Resize(UBound(arr) + 1, 1) = Cells(i, 1).Value
        Cells(i, 2).Resize(UBound(arr) + 1, 1) = Application.Transpose(arr)
    End If
Next i
End Sub

Hi Tony/Mark,

Yes this time it worked!

Thank you so much for both your help!

Regards,
Gehree
 
Upvote 0

Forum statistics

Threads
1,214,522
Messages
6,120,019
Members
448,938
Latest member
Aaliya13

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