text to columns

zs52411940

New Member
Joined
Jun 1, 2011
Messages
7
Hello guys,

I want to solve the following problem:

The original form is like this

Product(column 1) Style(column 2)
A1 long/short/medium
B short/medium
C long/short
D long
E short

I want to change it to:
Product Style
A1 long
A1 short
A1 medium
B short
B medium
C long
C short
D long
E short

I used to use Text to Column to transfer the second column according to "/", then copy it one by one, like
Product Style
A1 long short medium
But, as there are so many data, it is just not realistic to do it like this anymore. Can anybody help me figure out how to write a vba code to solve this problem? (I borrow books from library, but they are about Excel for numerical methods)

Thanks in advance
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Welcome to MrExcel board....

so I'm not sure I understand, but you have the data in the ]second column with the "/" that you would parse by? But you want the parsed data to go below in the next row under it? So by doing so you would push down the other rows?
 
Upvote 0
Welcome to MrExcel board....

so I'm not sure I understand, but you have the data in the ]second column with the "/" that you would parse by? But you want the parsed data to go below in the next row under it? So by doing so you would push down the other rows?


Yes exactly, my second column has data divided with "/". I want the parsed data to go below in the next row.
 
Upvote 0
Yes exactly, my second column has data divided with "/". I want the parsed data to go below in the next row
 
Upvote 0
ok so try this, but be sure to make a copy of your sheet before trying this
Code:
Sub separatefields()
Dim array1 As Variant
Dim counter As Integer
For Each Cell In Selection
counter = (Cell.Column + 1)
array1 = Split(Cell.Value, "/")
    For i = 0 To UBound(array1)
        If i > 2 And i < (UBound(array1) - 1) Then
            Cells(Cell.Row, counter).Value = Cells(Cell.Row, counter).Value & "/" & array1(i)
            If i = UBound(array1) - 2 Then
                counter = counter + 1
            End If
        Else
            Cells(Cell.Row, counter).Value = array1(i)
            counter = counter + 1
        End If
    Next
Next
End Sub
Sub ParseStyle()
Dim LR As Long, i As Long
Application.ScreenUpdating = False
LR = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row
Range("B2:B" & LR).Select
Call separatefields
Range("A1").Select
For i = LR To 2 Step -1
    x = Len(Cells(i, 2)) - Len(Application.WorksheetFunction.Substitute(Cells(i, 2).Value, "/", "")) + 1
    If x > 1 Then
        Range(Cells(i + 1, 2), Cells(i + x - 1, 2)).EntireRow.Insert
    End If
    
    For j = 1 To x
    
    Select Case j
    Case 1
        Cells(i, 1) = Cells(i, 1)
        Cells(i, 2) = Cells(i, 2 + j)
        Cells(i, 2 + j).Clear
    Case 2
        Cells(i + j - 1, 1) = Cells(i, 1)
        Cells(i + j - 1, 2) = Cells(i, 2 + j)
        Cells(i, 2 + j).Clear
    Case 3
        Cells(i + j - 1, 1) = Cells(i, 1)
        Cells(i + j - 1, 2) = Cells(i, 2 + j)
        Cells(i, 2 + j).Clear
    Case 4
        Cells(i + j, 1) = Cells(i, 1)
        Cells(i + j, 2) = Cells(i, 2 + j)
        Cells(i, 2 + j).Clear
    Case Else
    ' no data to parse
    End Select
    
    Next j
    
Next i
MsgBox "Done"
Application.ScreenUpdating = False
End Sub
 
Upvote 0
Thank you, MrExcel board is terrific.
One more question: How should I change the code if I have more columns with similar situation?
 
Upvote 0

Forum statistics

Threads
1,224,594
Messages
6,179,795
Members
452,943
Latest member
Newbie4296

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