How can I convert my Excel VBA code, which currently loops by each row, to loop through 10,000 sets, with each set containing 20 rows? I understand that the Step function might work, but I can't figure out how to amend my code to enable this. Basically, I would like to "paste" an array of 20 rows (by 21 columns) via the following lines of code:
And then "paste" the next 20 rows, and the following 20 rows and so forth.
Hope someone can help!
Full VBA code is shown below:
VBA Code:
.Range("CASHPASTE").Value = Application.Index(vInput1, r, 0)
.Range("EQPASTE").Value = Application.Index(vInput2, r, 0)
.Range("FIPASTE").Value = Application.Index(vInput3, r, 0)
Hope someone can help!
Full VBA code is shown below:
VBA Code:
Option Explicit
Sub Calc()
Dim r As Long, NoRows As Long, NoTrials As Long, NoPeriods As Long
Dim vInput1 As Variant, vInput2 As Variant, vInput3 As Variant
Dim vIRR As Variant, vExitIRR As Variant
With Worksheets("MCInput")
NoRows = .Cells(.Rows.Count, "B").End(xlUp).Row - .Range("TCASHRTN").Row + 1
NoTrials = WorksheetFunction.Max(Range("C:C")) 'Number of Trials
NoPeriods = WorksheetFunction.Max(Range("1:1")) - 1 'Number of Periods
vInput1 = .Range("TCASHRTN").Resize(NoRows).Value
vInput2 = .Range("TEQRTN").Resize(NoRows).Value
vInput3 = .Range("TFIRTN").Resize(NoRows).Value
End With
MsgBox NoTrials & " Trials over " & NoPeriods & " Periods" & " Rows = " & NoRows
ReDim vExitIRR(1 To NoTrials)
With Worksheets("Calcs")
For r = 1 To NoRows Step 20
.Range("CASHPASTE").Value = Application.Index(vInput1, r, 0)
.Range("EQPASTE").Value = Application.Index(vInput2, r, 0)
.Range("FIPASTE").Value = Application.Index(vInput3, r, 0)
vIRR = .Range("IRR").Value 'IRR is a single cell
vExitIRR(r, 1) = vIRR
Next r
End With
Worksheets("IRR").Range("B2").Resize(NoTrials, 1).Value = vExitIRR
End Sub