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

[TABLE="width: 200"]
<tbody>[TR]
[TD]Header[/TD]
[TD]Header[/TD]
[TD]Header[/TD]
[TD]Header[/TD]
[TD]Reference[/TD]
[TD]Quantity[/TD]
[/TR]
[TR]
[TD]There[/TD]
[TD]are[/TD]
[TD]some[/TD]
[TD]Apples[/TD]
[TD]Red,Green,Yellow,Blue[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]This[/TD]
[TD]is not[/TD]
[TD]an [/TD]
[TD]Orange[/TD]
[TD]White,Black[/TD]
[TD]2[/TD]
[/TR]
</tbody>[/TABLE]


to this


[TABLE="width: 200"]
<tbody>[TR]
[TD]Header[/TD]
[TD]Header[/TD]
[TD]Header[/TD]
[TD]Header[/TD]
[TD]Reference[/TD]
[TD]Quantity[/TD]
[/TR]
[TR]
[TD]There[/TD]
[TD]are[/TD]
[TD]some[/TD]
[TD]Apples[/TD]
[TD]Red[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]There[/TD]
[TD]are[/TD]
[TD]some[/TD]
[TD]Apples[/TD]
[TD]Green[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]There[/TD]
[TD]are[/TD]
[TD]some[/TD]
[TD]Apples[/TD]
[TD]Yellow[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]There[/TD]
[TD]are[/TD]
[TD]some[/TD]
[TD]Apples[/TD]
[TD]Blue[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]This[/TD]
[TD]is not[/TD]
[TD]an[/TD]
[TD]Orange[/TD]
[TD]White[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]This[/TD]
[TD]is not[/TD]
[TD]an[/TD]
[TD]Orange[/TD]
[TD]Black[/TD]
[TD]1[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
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,223,894
Messages
6,175,254
Members
452,623
Latest member
Techenthusiast

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