VBA copy cell ranges and paste each cell n times into a different sheet

Multim

New Member
Joined
Apr 17, 2024
Messages
14
Office Version
  1. 2021
Hello
I'm a VBA novice and normally can cobble something together but am stuck with this one.

I have a sheet called "Inventory Details" and a list of part numbers in cell A2 down, this will be a variable number of part numbers, I need each cell copied then pasted 6 times into a Sheet called " Routing Info" starting in A2 and the the next cell pasted 6 times below and so on.

Any assistance would be greatly appreciated.

"Inventory Details" Cells
Screenshot 2024-12-17 153428.png

" Routing Info" sheet final requirement
Screenshot 2024-12-17 153454.png


Thank you
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try this:
VBA Code:
Sub MyCopyData()

    Dim wsInv As Worksheet
    Dim wsRtg As Worksheet
    Dim lr As Long
    Dim r As Long
    
    Application.ScreenUpdating = False
    
'   Set worksheet objects
    Set wsInv = Worksheets("Inventory Details")
    Set wsRtg = Worksheets("Routing Info")
    
'   Find last row in column A with data on Inventory Detils sheet
    lr = wsInv.Cells(wsInv.Rows.Count, "A").End(xlUp).Row
    
'   Loop through all rows starting on row 2
    For r = 2 To lr
'       Populate next 6 rows on Routing Info sheet
        wsRtg.Range(wsRtg.Cells(((r - 2) * 6) + 2, "A"), wsRtg.Cells(((r - 2) * 6) + 7, "A")).Value = wsInv.Cells(r, "A").Value
    Next r
        
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Solution
Hello
I'm a VBA novice and normally can cobble something together but am stuck with this one.

I have a sheet called "Inventory Details" and a list of part numbers in cell A2 down, this will be a variable number of part numbers, I need each cell copied then pasted 6 times into a Sheet called " Routing Info" starting in A2 and the the next cell pasted 6 times below and so on.

Any assistance would be greatly appreciated.

"Inventory Details" Cells
View attachment 120419
" Routing Info" sheet final requirement
View attachment 120420

Thank you
Or this.

VBA Code:
Private Sub subCopyParts()
Dim arr() As Variant
Dim i As Integer
Dim lngRow As Long
Dim ii As Integer

  With Worksheets("Inventory Details")
    arr = .Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row).Value
  End With
  
  lngRow = 2
  
  For i = 1 To UBound(arr)
    For ii = 1 To 6
      Worksheets("Routing Info").Range("A" & lngRow).Value = arr(i, 1)
      lngRow = lngRow + 1
    Next ii
  Next i
  
End Sub
 
Upvote 0
You are welcome.
Glad we were able to help!
 
Upvote 0
Hi All

Re-opening this post to ask if the above code can be altered to paste until the last cell of data in column A?

Thanks in advance

Solved it, but thanks
 
Upvote 0
Re-opening this post to ask if the above code can be altered to paste until the last cell of data in column A?
My code (the one you marked as the solution) already does that, specifically this part here:
VBA Code:
'   Find last row in column A with data on Inventory Detils sheet
    lr = wsInv.Cells(wsInv.Rows.Count, "A").End(xlUp).Row

If it is not working the way you want, please show us an example where it is not working.
 
Upvote 0

Forum statistics

Threads
1,225,626
Messages
6,186,092
Members
453,337
Latest member
fiaz ahmad

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