VBA Split comma separated values in new rows and return the original description

Todor T

New Member
Joined
May 20, 2022
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
Hi all, here is what I am trying to do:

1653044301129.png1653044313524.png

I have comma separated values (Variants) mapped with unique description. I am trying to split the values in new rows, but keeping the original description.
This code from DanteAmor works great for the split, but I can not modify it to return the descriptions.

VBA Code:
Sub test_Comma()
  Dim n As Long
  Dim Cell As Range
  Dim itm As Variant
  n = Sheets(2).Range("B" & Rows.Count).End(3).Row
  For Each Cell In Sheets(1).Range("A1", Sheets(1).Range("A" & Rows.Count).End(3))
    For Each itm In Split(Cell, ",")    'Split cells 'iterate through the array
      n = n + 1
      Sheets(2).Range("B" & n).Value = Trim(itm)
    Next itm
  Next Cell
End Sub

Thanks in advance for you can help.
 

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)
@Todor T Welcome
With your input sheet as Tab1 and your Output sheet as Tab 2 test the following.

VBA Code:
Sub test_Comma()
  Dim n As Long
  Dim Cell As Range
  Dim itm As Variant
  n = Sheets(2).Range("B" & Rows.Count).End(3).Row
  For Each Cell In Sheets(1).Range("B1", Sheets(1).Range("B" & Rows.Count).End(3))
    For Each itm In Split(Cell, ",")    'Split cells 'iterate through the array
      n = n + 1
      Sheets(2).Range("A" & n).Value = Cell.Offset(0, -1).Value
      Sheets(2).Range("B" & n).Value = Trim(itm)
    Next itm
  Next Cell
End Sub

If you are not confident that those sheets will always remain in that tab order then it may be better to make reference to the sheets by name rather than tab index.
Hope that helps.
 
Upvote 0
@Todor T Welcome
With your input sheet as Tab1 and your Output sheet as Tab 2 test the following.

VBA Code:
Sub test_Comma()
  Dim n As Long
  Dim Cell As Range
  Dim itm As Variant
  n = Sheets(2).Range("B" & Rows.Count).End(3).Row
  For Each Cell In Sheets(1).Range("B1", Sheets(1).Range("B" & Rows.Count).End(3))
    For Each itm In Split(Cell, ",")    'Split cells 'iterate through the array
      n = n + 1
      Sheets(2).Range("A" & n).Value = Cell.Offset(0, -1).Value
      Sheets(2).Range("B" & n).Value = Trim(itm)
    Next itm
  Next Cell
End Sub

If you are not confident that those sheets will always remain in that tab order then it may be better to make reference to the sheets by name rather than tab index.
Hope that helps.
Thank you Snakehips!
It works perfect. I'll follow your advice for the references.
Best Regards Todor
 
Upvote 0
This can also be accomplished using Power Query aka Get and Transform Data and found on the Data Tab of the Ribbon.
1. Data-->Get and Transform-->From Range/Table
2. In the PQ Editor, Split Column-->By Delimiter-->Comma-->Advanced Options-->Rows--OK
3. On the PQ Editor Ribbon, Close and Load

No VBA or coding required.
 
Upvote 0
This can also be accomplished using Power Query aka Get and Transform Data and found on the Data Tab of the Ribbon.
1. Data-->Get and Transform-->From Range/Table
2. In the PQ Editor, Split Column-->By Delimiter-->Comma-->Advanced Options-->Rows--OK
3. On the PQ Editor Ribbon, Close and Load

No VBA or coding required.

Thank you for your reply and the Power Query alternative Alan!
The code is a part of a bigger macro and has to be VBA.

Best Regards Todor
 
Upvote 0

Forum statistics

Threads
1,214,945
Messages
6,122,393
Members
449,081
Latest member
JAMES KECULAH

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