Reputation: 513
I want to know what is better to populate the cells in a Range
:
For..Next
loop to go through them accessing the cells (see my code below), orVariant
array loaded with the cell data, or Here is my code:
' Count number of rows and columns we have
rowCounter = CountRowsFunction
colCounter = CountColsFunction
' Do operations needed
For i = 2 To rowCounter
originalSheet.Cells(i, colCounter + 1).Value = Round(DateDiff("n", originalSheet.Cells(i, ColumnsIndex(2)).Value, originalSheet.Cells(i, ColumnsIndex(3)).Value) / 60, 2)
Next i
In this case, I'm accessing the cells directly. But, I believe this may cause unnecessary calls to the spreadhseet. Thanks
Upvotes: 1
Views: 123
Reputation: 19319
The usage of an array to set multiple cell values is vastly superior. In the example below the range A1:MZ390
(152100 cells) are set with two methods:
Method 2 takes less than a second and method 1 takes > 4 seconds on my PC.
In the example it iterates the array, but you can use less code lines and just do varData = 2
- but it is unlikely that people want to set a bunch of cell values to a constant.
Option Explicit
Sub Test()
Dim dt1 As Date, dt2 As Date
Dim lngX As Long, lngY As Long
Dim varData As Variant
Dim ws As Worksheet
Dim rng As Range
'set ws
Set ws = ThisWorkbook.Worksheets("Sheet1")
'for loop method - without screen updating
Application.ScreenUpdating = False
ws.Cells.Delete
Set rng = ws.Range("A1:MZ390")
dt1 = Now
rng.Value = 1
For lngY = 1 To rng.Rows.Count
For lngX = 1 To rng.Columns.Count
rng.Cells(lngY, lngX).Value = 2
Next lngX
Next lngY
dt2 = Now
Application.ScreenUpdating = True
Debug.Print "For loop (without screen updating) took: " & Format(dt2 - dt1, "s") & " seconds"
'array method
ws.Cells.Delete
Set rng = ws.Range("A1:MZ390")
dt1 = Now
rng.Value = 1
varData = rng.Value
For lngX = 1 To UBound(varData, 1)
For lngY = 1 To UBound(varData, 2)
varData(lngX, lngY) = 2
Next lngY
Next lngX
rng.Value = varData
dt2 = Now
Debug.Print "Array method took: " & Format(dt2 - dt1, "s") & " seconds"
End Sub
Upvotes: 3
Reputation: 8531
Arrays are more efficient I believe, I use them when using a lot of data.
You'd use something like this, add a break point at the start of the loop, and use the locals window to view the array a.
Dim a() As Variant
a = Range("a1:a10").Value
For i = 1 To UBound(a)
Next i
Upvotes: 1