Need Help on VBA Code to Cut or Split Cell and Copy Row Based on Commas in Cell

JPatty

New Member
Joined
Sep 21, 2022
Messages
14
Office Version
  1. 365
  2. 2019
  3. 2011
Platform
  1. Windows
I'm trying to figure out some VAB code that loops through col B and C on ActiveSheet. For each cell that has a comma in B:C copy entire row containing that cell and insert below, then cut the numbers to the right of the comma and copy to the cell in the row below.
An example is attached as I don't think I can accurately describe what I am trying to accomplish.
Any help is greatly appreciated.




PlasticadeTemplate 4.xlsm
ABCDEFG
1
2Red
3Blue
4Black
5Yellow
6Pink
7
8
9DateTimeNumbers StartNumbers EndColor
101/11/202312:00985, 98960018Red
111/11/202312:00214-216, 22760018Red
121/11/202312:00086-089, 09560018Black
131/11/202312:00115-117, 120, 123, 12660018Yellow
141/11/202312:0013760018Yellow
151/11/202312:00245,24760018Yellow
161/11/202312:0060018985, 989Yellow
171/11/202312:0060018214-216, 227Pink
181/11/202312:0060018086-089, 095Pink
191/11/202312:0060018115-117, 120, 123, 126Pink
201/11/202312:0060018137Black
211/11/202312:0060018245,247Black
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Sheet1



PlasticadeTemplate 4.xlsm
ABCDEFG
1
2Red
3Blue
4Black
5Yellow
6Pink
7
8
9DateTimeNumbers StartNumbers EndColor
101/11/202312:0098560018Red
111/11/202312:0098960018Red
121/11/202312:00214-21660018Red
131/11/202312:0022760018Red
141/11/202312:00086-08960018Black
151/11/202312:0009560018Black
161/11/202312:00115-11760018Yellow
171/11/202312:0012060018Yellow
181/11/202312:0012360018Yellow
191/11/202312:0012660018Yellow
201/11/202312:0013760018Yellow
211/11/202312:0024560018Yellow
221/11/202312:0024760018Yellow
231/11/202312:0060018985Yellow
241/11/202312:0060018989Yellow
251/11/202312:0060018214-216Pink
261/11/202312:0060018227Pink
271/11/202312:0060018086-089Pink
281/11/202312:0060018095Pink
291/11/202312:0060018115-117Pink
301/11/202312:0060018120Pink
311/11/202312:0060018123Pink
321/11/202312:0060018126Pink
331/11/202312:0060018137Black
341/11/202312:0060018245Black
351/11/202312:0060018247Black
36
37
38
Sheet2
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try:
VBA Code:
Sub CopyRows()
    Application.ScreenUpdating = False
    Dim lRow As Long, v As Variant, i As Long, ii As Long, val As Variant, x As Long
    lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    v = Range("A10", Range("A" & Rows.Count).End(xlUp)).Resize(, 5).Value
    For i = UBound(v) To LBound(v) Step -1
        If InStr(v(i, 3), ",") > 1 Then
            val = Split(v(i, 3), ", ")
            Cells(i + 9, 3) = val(LBound(val))
            Cells(i + 10, 1).Resize(UBound(val)).EntireRow.Insert
            For ii = 1 To UBound(val)
                Cells(i + 9 + ii, 1).Resize(, 5).Value = Array(v(i, 1), v(i, 2), val(ii), v(i, 4), v(i, 5))
            Next ii
        Else
            If InStr(v(i, 4), ",") > 1 Then
                val = Split(v(i, 4), ", ")
                Cells(i + 9, 4) = val(LBound(val))
                Cells(i + 10, 1).Resize(UBound(val)).EntireRow.Insert
                For ii = 1 To UBound(val)
                    Cells(i + 9 + ii, 1).Resize(, 5).Value = Array(v(i, 1), v(i, 2), v(i, 3), val(ii), v(i, 5))
                Next ii
            End If
        End If
    Next i
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,092
Messages
6,123,064
Members
449,090
Latest member
fragment

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