copy and paste values with offset in columns vba code

RRM

New Member
Joined
Mar 10, 2020
Messages
5
Office Version
  1. 2013
Platform
  1. Windows
  2. MacOS
Hi, I have a function that generates a random number range based on a condition so I would like to copy the column range of these numbers (say column B1:B10) and paste it to another column in the same worksheet (say F1:10) once the condition is met and continue to copy the same column range (B1:B10) after the next iteration condition is met and paste into the next blank column (G1:G10) until 10 iterations, that is columns F to O are populated with the data range from column B1:B10. Is there a code I can perform this with? Thanks in advance.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Welcome to the MrExcel board!

I'm not entirely sure that I have understood what you want, but try this with a copy of your workbook.
If this is not it, please try to give more detail/examples.

VBA Code:
Sub Copy_Range()
  Dim nc As Long
  
  nc = IIf(IsEmpty(Range("F1").Value), 6, Cells(1, Columns.Count).End(xlToLeft).Column + 1)
  Cells(1, nc).Resize(10).Value = Range("B1:B10").Value
End Sub
 
Upvote 0
Hi, sorry for not replying sooner. I tried the code but I think I need to add more clarity so it helps to get a specific guidance:

I have the code that looks at the values generated from the random generator and once a condition is met then it stops. For example, it counts the numbers generated (for example 1, 2, 3) in range P1:P3 and once the condition value set in sheet1, A1 is met > 2, then it stops.

I would like to go further by running more iterations. That is once condition is met in sheet 1, A1 then it copy and paste values sheet 1, P1:P3 into sheet2, A1:A3 then goes back to sheet 1 and re-runs the below code. I would like to do this 10 times. Each time condition is met then it copy and paste value into sheet2, in next available column, B1:B3 then C1:C3 until sheet2, J1:J3 is populated with a range then END. Is there a nested DO UNTIL LOOP, IF THEN to use? I have no clue and appreciate guidance. Thanks in advance.

VBA Code:
Do
 Sheet1.Range("A1").Formula = "=COUNTIF($P$1:$P$3,0)"
 Loop Until Sheet1.Range("A1").Value > 2
End Sub
 
Upvote 0
Hi, I found a reply that worked. Thanks to all.
 
Upvote 0

Forum statistics

Threads
1,214,627
Messages
6,120,610
Members
448,973
Latest member
ChristineC

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