VBA to copy/paste data via loop n times where n= a cell value

mgerken79

New Member
Joined
Jan 13, 2013
Messages
1
I have 2 separate workbooks; one (let's call it 'Orders') is used for order tracking, the second (let's call it 'Export') is an export file that contains details of the orders received that day. There could be 1 to ‘x’ orders received in a day. The data from ‘Export’ on worksheet(“Sheet1”) in columns A:S needs to be copied and pasted into ‘Orders’ on worksheet(“Order Data”) into columns H:Z starting in the first row where column H is blank.

Here’s where things get over my head: I need to accomplish this by copy and pasting one row at a time because each row needs to be pasted n times where n= the value located in column S. For instance, if S2=’4’:

  1. I need the loop to select the array Workbooks(“Export”).Worksheets(“Sheet1”).Range(“A2:S2”)
  2. Find the first blank cell in Workbooks(“Orders”).Worksheets(“Order Data”).Column(“H”)
  3. Paste the array in that row and the next 3 rows (so the row is pasted a total of 4 times, equal to the value found in “S2”)
  4. Then repeat for the ‘x’ number of orders in the “Export” file

Hopefully that makes some sense but please let me know if you have any questions or a more complete example would be helpful. Thanks so much in advance.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
This line of code might work if adapted to your code. It assumes a variable named "c As Range" in a For Next loop. If you had posted your code, this would be a lot cleaner, but hopefully you get the idea.

Code:
lr = Sheets("Orders").Cells(Rows.Count, "H").End(xlUp).Row + 1
n = Sheets("Sheet1").Range("S" & c.Row)
c.Resize(1, 19).Copy Sheets("Orders").Range("H" & lr).Resize(n, 19)
Code:
 
Upvote 0

Forum statistics

Threads
1,216,073
Messages
6,128,637
Members
449,461
Latest member
kokoanutt

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