split string separated comma

Forestq

Active Member
Joined
May 9, 2010
Messages
482
Hi,

in filed I have couple of value separated by comma like below:

A1 header1
B1 header2
C1 header3
Audi
592035, 579733, 653749, 579735
20 000

<tbody>
</tbody>

If my macro found that string (always will be separate by ",") should split the string and add rows (= to number of string). The output should be as below:

A1 header1
B1 header2
C1 header3
Audi
592035
20 000
Audi
579733
20 000
Audi
653749
20 000
Audi
579735
20 000

<tbody>
</tbody>

I have:
Code:
 Set sourceWb = ActiveWorkbook
  Set ws = sourceWb.Worksheets(1)
  
  Dim LastRow As Long
  Dim MY_Split As Variant
   
  LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
 
    For i = 2 To LastRow
        If InStr(Range("D" & i).Value, ",") Then 'find the string
            
            MY_Split = Split(Range("D" & i).Value, ",") 'split the string
            
            'MsgBox UBound(MY_Split)
            'how to add here the proper count of rows???        

                    'For countIndex = LBound(MY_Split) To UBound(MY_Split) 'here I can fill the table                                 
                    'Next
        End If
    Next i
End Sub

If you know how can I add the proper number of rows please help me.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I added the proper number of rows but I don`t know how to put there the value :(

Code:
Set sourceWb = ActiveWorkbook
  Set ws = sourceWb.Worksheets(1)
  
  Dim LastRow As Long
  Dim MY_Split As Variant
   
  LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
 
    For i = 2 To LastRow
        If InStr(Range("D" & i).Value, ",") Then 'find the string
            
            MY_Split = Split(Range("D" & i).Value, ",") 'split the string
            
                    For countIndex = LBound(MY_Split) To UBound(MY_Split)
                          ActiveCell.Offset(1).EntireRow.Insert
                    Next
        End If
    Next i
 
Upvote 0

Forum statistics

Threads
1,214,948
Messages
6,122,420
Members
449,083
Latest member
Ava19

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