views:

56

answers:

1

From one of VBA tutorials I learned that variables contining numbers should be firstly declared as integers:

Dim mynumber as integer

But, please, look at this code:

Sub math()
   A = 23
   B = 2
   ABSumTotal = A + B
   strMsg = "The answer is " & "$" & ABSumTotal & "."
   MsgBox strMsg
   strMsg = "The answer is " & "$" & Sqr(ABSumTotal) & "."
   MsgBox strMsg 
End Sub

No variables are declared here as integer, but it still works just fine. Why is it so?

+4  A: 

By default, VB does not require variable declarations. This has caused lots of frustration because it means that typos go undetected until something breaks at runtime.

To change this, add Option Explicit to the top of the file.

SLaks
Aaah, I see. Thank you.
brilliant
+1 Basically right, but the VBA syntax is "Option Explicit" not "Option Explicit On". And Option Strict does not exist in VBA (I think it's VB.NET).
Joe
Fixed ; thanks.
SLaks