Copy and paste string separated by commas

rex759

Well-known Member
Joined
Nov 8, 2004
Messages
608
Office Version
  1. 365
Platform
  1. Windows
Hello,
The code below inserts x number of rows based on the number in column A. Then it copes the information (Columns C:E) from above down into the new rows. I want to add the location numbers from Column G at the same time. If there is more than one location in Column G, they are always separated by a comma (no spaces). When there is only one location number in Column G, I can get it to work. I am not sure how to go about grabbing the numbers between the commas.

<Before Macro>
HD Pro Test 154.xlsm
ABCDEFG
1LOCATION#ITEMDESCRIPTIONQTY2nd LOCATIONS
202807F0510139050-16921/4 REPLCMNT STP 3/8OD15
323232A0180166986-70065' BI-FOLD DOOR HARDWA151819Z01,1810Z01
480166986-70065' BI-FOLD DOOR HARDWA
580166986-70065' BI-FOLD DOOR HARDWA
601916Z01100344558100344558I606 11/2 CXC 45 ELB W15
734008D0244618BN-44618*GALV PIPE 3/4 X 36164719y01,2310Z01,1921543
844618BN-44618*GALV PIPE 3/4 X 36
944618BN-44618*GALV PIPE 3/4 X 36
1044618BN-44618*GALV PIPE 3/4 X 36
1102510D0135897853049323259X3/4" WOOL ROLLER COV7
1201817K0580336384-0293KWIK #293 BRASS ROUND4
Sheet22


<After Macro>
HD Pro Test 154.xlsm
ABCDEFG
16LOCATION#ITEMDESCRIPTIONQTY2nd LOCATIONS
1702807F0510139050-16921/4 REPLCMNT STP 3/8OD15
1823232A0180166986-70065' BI-FOLD DOOR HARDWA151819Z01,1810Z01
191810Z0180166986-70065' BI-FOLD DOOR HARDWA
201819Z0180166986-70065' BI-FOLD DOOR HARDWA
2101916Z01100344558100344558I606 11/2 CXC 45 ELB W15
2234008D0244618BN-44618*GALV PIPE 3/4 X 36164719y01,2310Z01,1921543
23192154344618BN-44618*GALV PIPE 3/4 X 36
242310Z0144618BN-44618*GALV PIPE 3/4 X 36
254719y0144618BN-44618*GALV PIPE 3/4 X 36
2602510D0135897853049323259X3/4" WOOL ROLLER COV7
2701817K0580336384-0293KWIK #293 BRASS ROUND4
Sheet22



VBA Code:
'Information based on column number
 'Set the range length of the row
 Set r = Worksheets("Sheet2").Range("A:I")
 
 'macro starts at the bottom of the sheet
 LastRow = 76
 
 Worksheets("Sheet2").Select
 'Pick column to determine number of rows to add
 For n = LastRow To 1 Step -1
 temp = Range("A" & n)
 
    If (temp > 0) Then
        Rows(n + 1 & ":" & n + temp).Insert Shift:=xlDown
        'add C, D & E
        
        If temp = 1 Then
            Range("B" & n + 1) = Cells(n, "G")
            Range("C" & n + 1) = Cells(n, "C")
            Range("D" & n + 1) = Cells(n, "D")
            Range("E" & n + 1) = Cells(n, "E")
        End If
              
             If temp = 2 Then
                Range("C" & n + 1) = Cells(n, "C")
                Range("D" & n + 1) = Cells(n, "D")
                Range("E" & n + 1) = Cells(n, "E")
                Range("C" & n + 2) = Cells(n, "C")
                Range("D" & n + 2) = Cells(n, "D")
                Range("E" & n + 2) = Cells(n, "E")
             End If
        If temp = 3 Then
                Range("C" & n + 1) = Cells(n, "C")
                Range("D" & n + 1) = Cells(n, "D")
                Range("E" & n + 1) = Cells(n, "E")
                Range("C" & n + 2) = Cells(n, "C")
                Range("D" & n + 2) = Cells(n, "D")
                Range("E" & n + 2) = Cells(n, "E")
                Range("C" & n + 3) = Cells(n, "C")
                Range("D" & n + 3) = Cells(n, "D")
                Range("E" & n + 3) = Cells(n, "E")
        End If
    End If
  Next n

Any help is appreciated
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
How about
VBA Code:
Sub rex()
   Dim i As Long, Rws As Long
   
   With Worksheets("Sheet2")
      For i = .Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
         Rws = .Cells(i, 1).Value
         If Rws > 0 Then
            .Rows(i + 1).Resize(Rws).Insert
            .Range("C" & i).Resize(Rws + 1, 3).FillDown
            .Range("B" & i + 1).Resize(Rws).Value = Application.Transpose(Split(.Cells(i, 7).Value, ","))
         End If
      Next i
   End With
End Sub
 
Upvote 0
Solution
Wow, much more elegant and efficient. Works perfectly. Thank you!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,691
Members
448,978
Latest member
rrauni

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