Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top