VBA to Reverse a string of delimited and hyphenated text.

decadence

Well-known Member
Joined
Oct 9, 2015
Messages
525
Office Version
  1. 365
  2. 2016
  3. 2013
  4. 2010
  5. 2007
Platform
  1. Windows
Hi, Is it possible to split hyphenated and delimited text into the next row but keep the data in the same rows?

example

From This

WhatWho WhenWhy
ApplesPieSomethingA1-A3,A5,A7-A10
BananaSplitNothingD1,D2,D8-D9
Candy CaneEverythingB1,B3-B6

<tbody>
</tbody>


To This

WhatWhoWhenWhy
ApplesPieSomethingA1
ApplesPieSomethingA2
ApplesPieSomethingA3
ApplesPieSomethingA5
ApplesPieSomethingA7
ApplesPieSomethingA8
ApplesPieSomethingA9
ApplesPieSomethingA10
BananaSplitNothingD1
BananaSplitNothingD2
BananaSplitNothingD8
BananaSplitNothingD9
CandyCaneEverythingB1
CandyCaneEverythingB3
CandyCaneEverythingB4
CandyCaneEverythingB5
CandyCaneEverythingB6

<tbody>
</tbody>
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Try this for result starting "F1".
Code:
[COLOR="Navy"]Sub[/COLOR] MG07Nov15
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, Sp [COLOR="Navy"]As[/COLOR] Variant, SSp [COLOR="Navy"]As[/COLOR] Variant, Ray(), 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]
[COLOR="Navy"]Dim[/COLOR] oLet [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] Fst [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Lst [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
ReDim Ray(1 To 4, 1 To 1)
Ray(1, 1) = "What": Ray(2, 1) = "Who": Ray(3, 1) = "When": Ray(4, 1) = "Why"
c = 1
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    Sp = Split(Dn.Offset(, 3).Value, ",")
    [COLOR="Navy"]For[/COLOR] n = 0 To UBound(Sp)
        [COLOR="Navy"]If[/COLOR] InStr(Sp(n), "-") > 0 [COLOR="Navy"]Then[/COLOR]
                SSp = Split(Sp(n), "-")
                oLet = Left(Sp(n), 1)
                Fst = Replace(SSp(0), oLet, "")
                Lst = Replace(SSp(1), oLet, "")
                [COLOR="Navy"]For[/COLOR] nn = Fst To Lst
                    c = c + 1
                    ReDim Preserve Ray(1 To 4, 1 To c)
                    Ray(1, c) = Dn.Value: Ray(2, c) = Dn.Offset(, 1).Value
                    Ray(3, c) = Dn.Offset(, 2).Value: Ray(4, c) = oLet & nn
                [COLOR="Navy"]Next[/COLOR] nn
        [COLOR="Navy"]Else[/COLOR]
                    c = c + 1
                    ReDim Preserve Ray(1 To 4, 1 To c)
                    Ray(1, c) = Dn.Value: Ray(2, c) = Dn.Offset(, 1).Value
                    Ray(3, c) = Dn.Offset(, 2).Value: Ray(4, c) = Sp(n)
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] n
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]With[/COLOR] Range("F1").Resize(c, 4)
    .Value = Application.Transpose(Ray)
    .Borders.Weight = 2
    .Columns.AutoFit
 [COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Thank you MickG, You sir are a legend.
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,791
Members
451,589
Latest member
Harold14

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