I’ve been having trouble trying to optimize a certain part of my code. I am performing a Monte Carlo simulation and I want to copy the values of a range a repeated number of times. To do this, I’ve used a For Each In structure. Below is a minimal example.
sub example()
Dim live As Excel.Range 'the range to be copied in each For Each In
Dim acell as Excel.Range 'For range
Set live = Range("C5:P5")
For Each acell in Range("B9:B90")
acell.value=live.value
Next acell
End Sub
The problem is that live spans multiple columns, while acell is just one cell; what ends up happening is just the first column is copied, the rest are blank. I have also used For Each acell in XYZ.rows where XYZ is a previously defined range of multiple columns and rows. However, this is considerably slower.
Is it possible to run through a single-column range and paste multiple columns, starting with that first cell?
You’re almost there; you’re just missing a tiny change in your code. You have to
Resizethe target range. Instead ofuse