Before we look at the different variable declarations, we need to establish the difference between the words explicit and implicit.
- explicit is defined as: "stated clearly and in detail, leaving no room for confusion or doubt."
- implicit is defined as: "implied, rather than expressly stated"
When coding in Visual Basic, you are by default not obligated to declare your variables before using them. This an example of an implicit variable declaration.
When doing so, VBA will automatically declare it as a variant type provided that the variable has not been declared before. This is a nice feature, but may cause trouble when writing larger scripts. Let's look at an example: We have already written a lot of code using the variable "MyVar". If we were to misspell "MyVar" with "Myvar" in one line, VBA will think of it as a new variable, and declare it automatically, thus not informing us.
MyVar = 4
'We want to multiply MyVar with 10
myvar = MyVar * 10
'... lots of code
'When we finally display MyVar, thinking it equals 40
MsgBox MyVar
'4 is displayed