I am currently trying out code for timing efficiency (how fast it runs, basically).
The general consensus is that code with ActiveCell,.Select,Selection and so forth are basicly rejected for being slow and buggy, whereas code that uses variables and not ActiveCell,.Select,Selection are considered quicker and less buggy
The two codes I have made do the same thing, which is to enter a number 40000+1 every consecutive cell (so Cell A1 is 40000 then A2 is 40001 and so forth) and then converts those numbers into dates. One does this with ActiveCell, Selection and Select, while the other does it with varibles,Long and Range. I also did both with and then without Application.ScreenUpdating = False to see how that worked as well.
The code ActiveCell etc. ran at 13494,26567,26489,14040,26598(without Application.ScreenUpdating = False) and 1154,1123,1123,1107,1170 (with Application.ScreenUpdating = False) "Milliseconds"
The code without ActiveCell ran at 905,905,905,671,687 (without Application.ScreenUpdating = False) and ran at 577,609,577,577,562 (with Application.ScreenUpdating = False) "Milliseconds"
The milliseconds is in "Milliseconds" as the Private Declare Function GetTickCount Lib "kernel32.dll" () As Long I have been lead to believe isnt very accurate, I use it because the accurate ones are addins for excel and I unable to download or install anything on this PC, but gvies a decent idea of the speed the code runs at
The code that the efficency tests is the For i = 1 To 1000 loop
Code with ActiveCells:
Private Declare Function GetTickCount Lib "kernel32.dll" () As Long
Sub UsingVaribles()
Dim NumberToday As Long
Dim StartTimer As Long
Dim rngCells As Range
Dim rng As Range
Application.ScreenUpdating = False
[A1].Select
NumberToday = 40000
StartTimer = GetTickCount
k = 1
For q = 1 To 26
For i = 1 To 1000
NumberToday = NumberToday + 1
Set rngCells = Cells(i, k)
rngCells.Value = NumberToday
Next i
Range(rngCells, Cells(1, k)).NumberFormat = "m/d/yyyy" ' 1 bug out
k = k + 1
Next q
MsgBox (GetTickCount - StartTimer & " Milliseconds")
End Sub
Code without ActiveCells:
Sub UsingActivecell()
Application.ScreenUpdating = False
[A1].Select
NumberToday = 40000
Dim StartTimer As Long
StartTimer = GetTickCount
For q = 1 To 26
For i = 1 To 1000
NumberToday = NumberToday + 1
ActiveCell.Value = NumberToday
ActiveCell.Offset(1, 0).Select
Next i
ActiveCell.Offset(-1, 0).Select
Range(Selection, Selection.End(xlUp)).Select ' 3 issues with bug out, due to my incompitence
Selection.NumberFormat = "m/d/yyyy"
ActiveCell.Offset(0, 1).Select
ActiveCell.End(xlUp).Select
Next q
MsgBox (GetTickCount - StartTimer & " Milliseconds")
End Sub
ActiveCellor notActiveCellVBA based code, as much as I have looked around I havent been able to find any concrete evidense that showsActiveCelldoes or doesnt run quicker(other than a large quantity of people saying so) \$\endgroup\$