Split Rows data into new worksheet rows with specify words length

monkeyz

New Member
Joined
Mar 17, 2018
Messages
2
Hello, Good Day,
I need to split many rows data and save into new worksheet rows,
First, split the data with delimiter"|||", and I can select length in each split data, (in the example I choose text length is 16)

Original Data need to be split .

LabelOriginal Data
Label APeter 11AA, 22BB, 33CC,|||Ann ABC, 115C,|||Nick P122, C502, 607,
Label BMonkey 1356, 28BC, ABC, 68ED, P2,|||Jordon N134, 1C,|||Nelson 6B, 90D2, 2001A, 22BB,|||May P112, C702, E607,

<tbody>
</tbody>
....
....
....


After split, I want the data, as below:


LabelSplit Data
Label APeter 11AA,
Label APeter 22BB,
Label APeter 33CC,
Label AAnn ABC, 115C,
Label ANick P122, C502,
Label ANick 607,
Label BMonkey 1356,
Label BMonkey 28BC,
Label BMonkey ABC,
Label BMonkey 68ED, P2,
Label BJordon N134, 1C,
Label BNelson 6B, 90D2,
Label BNelson 2001A,
Label BNelson 22BB,
Label BMay P112, C702,
Label BMay E607,

<tbody>
</tbody>



Thank you for your time
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Try this for results starting "D1".
NB:- There appears to be some inconsistencies in your data in second column of results like:-
Nick P122, C502,
Nick 607,
The above should, perhaps should be 3 lines "Nick P122" then "Nick C502", then "Nick 607"


Code:
[COLOR=navy]Sub[/COLOR] MG17Mar49
[COLOR=navy]Dim[/COLOR] Rng [COLOR=navy]As[/COLOR] Range, Dn [COLOR=navy]As[/COLOR] Range, Sp [COLOR=navy]As[/COLOR] Variant, SPa [COLOR=navy]As[/COLOR] Variant
[COLOR=navy]Dim[/COLOR] n [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] nn [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] c [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] Ray(), Nam [COLOR=navy]As[/COLOR] [COLOR=navy]String[/COLOR]
[COLOR=navy]Set[/COLOR] Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
[COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] Dn [COLOR=navy]In[/COLOR] Rng
    Sp = Split(Dn.Offset(, 1), "|||")
        [COLOR=navy]For[/COLOR] n = 0 To UBound(Sp)
            SPa = Split(Sp(n), ", ")
            [COLOR=navy]For[/COLOR] nn = 0 To UBound(SPa)
                    
                    [COLOR=navy]If[/COLOR] nn = 0 [COLOR=navy]Then[/COLOR]
                    Nam = Split(SPa(0), " ")(0)
                    SPa(0) = Split(SPa(0), " ")(1)
                    [COLOR=navy]End[/COLOR] If
                    c = c + 1
                    ReDim Preserve Ray(1 To 2, 1 To c)
                    Ray(1, c) = Dn.Value: Ray(2, c) = Nam & ", " & SPa(nn)
            [COLOR=navy]Next[/COLOR] nn
        [COLOR=navy]Next[/COLOR] n
[COLOR=navy]Next[/COLOR] Dn
Range("D1").Resize(c, 2).Value = Application.Transpose(Ray)
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick


<tbody>
</tbody>
 
Last edited:
Upvote 0
Hello, Mick,
Thanks,
because I also want to control the words length, and in the case, I allow the max text length is 16,
so After split, some column contains multiple element.
such as below output array length is 14, 16,
Label AAnn ABC, 115C,
Label ANick P122, C502,


How can I add the function?


Many Thanks,

<tbody>
</tbody>
 
Upvote 0
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG18Mar27
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, sp [COLOR="Navy"]As[/COLOR] Variant, SPa [COLOR="Navy"]As[/COLOR] Variant, Txt [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] nn [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Ray(), Nam [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] Xt [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range("A2", Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    sp = Split(Dn.Offset(, 1), "|||")
        [COLOR="Navy"]For[/COLOR] n = 0 To UBound(sp)
            SPa = Split(sp(n), ", ")
            [COLOR="Navy"]For[/COLOR] nn = 0 To UBound(SPa)
                    [COLOR="Navy"]If[/COLOR] nn = 0 [COLOR="Navy"]Then[/COLOR]
                        Nam = Split(SPa(0), " ")(0)
                        Txt = Nam
                    [COLOR="Navy"]End[/COLOR] If
             
                    Xt = IIf(nn = 0, Split(SPa(0), " ")(1), SPa(nn))
                  
                    [COLOR="Navy"]If[/COLOR] Len(Nam & "," & Xt) < 16 [COLOR="Navy"]Then[/COLOR]
                            Nam = Nam & ", " & Xt
                    [COLOR="Navy"]ElseIf[/COLOR] nn < UBound(SPa) + 1 [COLOR="Navy"]Then[/COLOR]
                            c = c + 1
                            ReDim Preserve Ray(1 To 2, 1 To c)
                            Ray(1, c) = Dn.Value: Ray(2, c) = Nam
                            Nam = Txt & ", " & SPa(nn)
                   [COLOR="Navy"]End[/COLOR] If
           [COLOR="Navy"]Next[/COLOR] nn
                   [COLOR="Navy"]If[/COLOR] nn = UBound(SPa) + 1 [COLOR="Navy"]Then[/COLOR]
                        c = c + 1
                        ReDim Preserve Ray(1 To 2, 1 To c)
                        Ray(1, c) = Dn.Value: Ray(2, c) = Nam
                   [COLOR="Navy"]End[/COLOR] If
     [COLOR="Navy"]Next[/COLOR] n
[COLOR="Navy"]Next[/COLOR] Dn
Range("D1").Resize(c, 2).Value = Application.Transpose(Ray)
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,520
Members
449,088
Latest member
RandomExceller01

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