I am interested in learning the fastest way to execute a set of instructions in a double loop for cyclic conversion to a two-dimensional range of cells. My code would be like this:
Sub Test()
For i = 1 To 1000000
For j = 1 To 10 'It can be more than 10
'I put a set of instructions here
Next j
Next i
End Sub
For example, suppose I write simple code to accomplish the following task:
Sub Test1()
T0 = Timer
For i = 1 To 1000000
For j = 1 To 10
Cells(i, j) = j + Rnd()
Next j
Next i
InputBox "The runtime of this program is ", "Runtime", Timer - T0
End Sub
Test1 , 179.6406 . (i j), Test1 , Variant. Test1, Longs, VBA Longs. Test2 , 168.7539 ( 11 ).
Test2, Excel, , Test2.
Sub Test3()
Dim i As Long, j As Long
T0 = Timer
ScreenUpdateState = Application.ScreenUpdating
StatusBarState = Application.DisplayStatusBar
CalcState = Application.Calculation
EventsState = Application.EnableEvents
DisplayPageBreakState = ActiveSheet.DisplayPageBreaks
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False
For i = 1 To 1000000
For j = 1 To 10
Cells(i, j) = j + Rnd()
Next j
Next i
Application.ScreenUpdating = ScreenUpdateState
Application.DisplayStatusBar = StatusBarState
Application.Calculation = CalcState
Application.EnableEvents = EventsState
ActiveSheet.DisplayPageBreaks = DisplayPageBreaksState
InputBox "The runtime of this program is ", "Runtime", Timer - T0
End Sub
Test2 Test3 96.13672 . , . - ? , .