Looping through an array is way faster than looking through a range.
See my test below:
Option Explicit
Const strRANGE_ADDRESS As String = "A1:A100000"
Sub LoopRangeAddOne()
Dim r As Range
Dim lStart As Double
Dim lEnd As Double
lStart = Timer
For Each r In Range(strRANGE_ADDRESS)
r.Value = r.Value + 1
Next r
lEnd = Timer
Debug.Print "Duration = " & (lEnd - lStart) & " seconds"
End Sub
Sub LoopArrayAddOne()
Dim varArray As Variant
Dim var As Variant
Dim lStart As Double
Dim lEnd As Double
lStart = Timer
varArray = Range(strRANGE_ADDRESS).Value
For Each var In varArray
var = var + 1
Next var
Range(strRANGE_ADDRESS).Value = varArray
lEnd = Timer
Debug.Print "Duration = " & (lEnd - lStart) & " seconds"
End Sub
Results:
LoopRangeAddOne Duration = 2.2734375 seconds
LoopArrayAddOne Duration = 0.08203125 seconds
Which makes looping through an array 96.39% faster than through a loop.
Hope this helps :)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…