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.
[TABLE="width: 500"]
<tbody>[TR]
[TD]Name[/TD]
[TD]Attributes[/TD]
[/TR]
[TR]
[TD]Tom[/TD]
[TD]Male, Teen, Tall, Glasses, Black hair[/TD]
[/TR]
[TR]
[TD]Henry[/TD]
[TD]Male, Adult, Short[/TD]
[/TR]
[TR]
[TD]Clarissa[/TD]
[TD]Female, Adult[/TD]
[/TR]
[TR]
[TD]Sam[/TD]
[TD]Male, Child, Short, Black hair[/TD]
[/TR]
[TR]
[TD]Sarah[/TD]
[TD]Female, Teen, Red hair[/TD]
[/TR]
</tbody>[/TABLE]

I require it to look something like this.
[TABLE="width: 500"]
<tbody>[TR]
[TD]Name[/TD]
[TD]Attributes[/TD]
[/TR]
[TR]
[TD]Tom[/TD]
[TD]Male[/TD]
[/TR]
[TR]
[TD]Tom
[/TD]
[TD]Teen[/TD]
[/TR]
[TR]
[TD]Tom[/TD]
[TD]Tall[/TD]
[/TR]
[TR]
[TD]Tom[/TD]
[TD]Glasses[/TD]
[/TR]
[TR]
[TD]Tom[/TD]
[TD]Black hair[/TD]
[/TR]
[TR]
[TD]Henry[/TD]
[TD]Male[/TD]
[/TR]
[TR]
[TD]Henry[/TD]
[TD]Adult[/TD]
[/TR]
[TR]
[TD]Henry[/TD]
[TD]Short[/TD]
[/TR]
[TR]
[TD]Clarissa[/TD]
[TD]Female[/TD]
[/TR]
[TR]
[TD]Clarissa[/TD]
[TD]Adult[/TD]
[/TR]
[TR]
[TD]Sam[/TD]
[TD]Male[/TD]
[/TR]
[TR]
[TD]Sam[/TD]
[TD]Child[/TD]
[/TR]
[TR]
[TD]Sam[/TD]
[TD]Short[/TD]
[/TR]
[TR]
[TD]Sam[/TD]
[TD]Black hair[/TD]
[/TR]
[TR]
[TD]Sarah[/TD]
[TD]Female[/TD]
[/TR]
[TR]
[TD]Sarah[/TD]
[TD]Teen[/TD]
[/TR]
[TR]
[TD]Sarah[/TD]
[TD]Red hair[/TD]
[/TR]
</tbody>[/TABLE]

Any help would be greatly appreciated!

Thank you!
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
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,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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