Anirudh Ramanathan
Anirudh Ramanathan

Reputation: 46728

VBA Macros: Using variables with type modifier

I have declared an integer variable testInt in shorthand notation as follows

Dim testInt% 

Is there any difference between using

somevalue = testInt * testInt

versus

somevalue = testInt% * testInt%

In short, is there an advantage of using the type-specifier at every point the variable is referenced?

Upvotes: 2

Views: 647

Answers (1)

brettdj
brettdj

Reputation: 55672

A quick time test shows they are line ball - which intuitively makes sense

Using Long rather than Integer will be more efficient. See this MSFT Link

I will repeat this with a more accurate API timer

Sub B()
Dim testInt%
Dim somevalue%
Dim lcnt As Long
Dim dbStart As Double
dbStart = Timer()
For lcnt = 1 To 100000000
somevalue = testInt * testInt
Next
MsgBox "Time was " & Timer() - dbStart
End Sub

Sub A()
Dim testInt%
Dim somevalue%
Dim lcnt As Long
Dim dbStart As Double
dbStart = Timer()
For lcnt = 1 To 100000000
somevalue = testInt% * testInt%
Next
MsgBox "Time for type-specified was " & Timer() - dbStart
End Sub

Upvotes: 5

Related Questions