Array to speed up (Range vs Array)

Below two procedures are different ways of populating a range of cells. The one at the bottom using arrays is the faster one. We can use the same to do processing/calculations in arrays and then put the calculated data back to range to speed up the process. Copy these procedures in a general module and run them (F5).
Sub PopulatingRangeEachCell() 'This procedure populates the range one cell at time Dim rng As Range Dim rngCell As Range Dim lngCnt As Long Dim stTime As Date stTime = Now Set rng = Sheet1.Range("A1:A100000") For Each rngCell In rng lngCnt = lngCnt + 1 rngCell.Value = lngCnt Next rngCell Set rng = Nothing Set rngCell = Nothing MsgBox "Start Time: " & Format(stTime, "hh:mm:ss") & vbCrLf & _ "End Time: " & Format(Now, "hh:mm:ss") & vbCrLf & _ DateDiff("s", stTime, Now) & " seconds", vbInformation, "Time Taken" End Sub Sub PopulatingRangeUsingArray() 'This procedure assigns range to array 'Then populates array and 'then assign array back to range Dim rng As Range Dim lngCnt As Long Dim stTime As Date Dim arr stTime = Now Set rng = Sheet1.Range("A1:A100000") arr = rng For lngCnt = LBound(arr, 1) To UBound(arr, 1) arr(lngCnt, 1) = lngCnt Next lngCnt rng = arr Erase arr Set rng = Nothing MsgBox "Start Time: " & Format(stTime, "hh:mm:ss") & vbCrLf & _ "End Time: " & Format(Now, "hh:mm:ss") & vbCrLf & _ DateDiff("s", stTime, Now) & " seconds", vbInformation, "Time Taken" End Sub
»
- Vishesh's blog
- Add new comment
- 1257 reads

Recent comments
6 hours 33 min ago
9 hours 56 min ago
10 hours 19 min ago
16 hours 22 min ago
17 hours 13 min ago
1 day 8 hours ago
1 day 8 hours ago
1 day 11 hours ago
1 day 19 hours ago
2 days 14 hours ago