VBA Transpose and separate comma separated values

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, I am trying to separate comma separated text in multiple cells in a column but would like to retain the exact same date for each row of separated text except for Quantity and the transposed references. Also I will have varied rows of data in the reference column, Can some one help with this please.

From this

HeaderHeaderHeaderHeaderReferenceQuantity
TherearesomeApplesRed,Green,Yellow,Blue4
Thisis notan OrangeWhite,Black2

<tbody>
</tbody>


to this


HeaderHeaderHeaderHeaderReferenceQuantity
TherearesomeApplesRed1
TherearesomeApplesGreen1
TherearesomeApplesYellow1
TherearesomeApplesBlue1
Thisis notanOrangeWhite1
Thisis notanOrangeBlack1

<tbody>
</tbody>
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Excel Workbook
ABCDEF
2TherearesomeApplesRed, Green,Yellow,Blue5
Sheet1
Excel 2010

Will the number of reference always equal quantity, or might the above occur?
 
Upvote 0
Try this for results sting "H1".
Code:
[COLOR="Navy"]Sub[/COLOR] MG04Sep44
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, Sp [COLOR="Navy"]As[/COLOR] Variant, c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
c = 1
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    Sp = Split(Dn.Offset(, 4), ",")
    Cells(c, "H").Resize(UBound(Sp) + 1, 4).Value = Dn.Resize(, 4).Value
    Cells(c, "L").Resize(UBound(Sp) + 1).Value = IIf(UBound(Sp) > 0, Application.Transpose(Sp), Dn.Offset(, 4).Value)
    Cells(c, "M").Resize(UBound(Sp) + 1).Value = 1
    c = c + UBound(Sp) + 1
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Hi GTO, Yes it may occur as the data is varied.

Hi MickG, Your code works, but can it be changed so the headers are not included so it starts in row 2, also is it possible to do it in the same columns and get the same result as your code? Many Thanks
 
Upvote 0
Change the second line to range("A2") from range("A1").

also is it possible to do it in the same columns and get the same result as your code?

I'm not sure what this means ???
 
Upvote 0

Forum statistics

Threads
1,215,262
Messages
6,123,953
Members
449,135
Latest member
jcschafer209

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