MOCKSTACKS
EN
Questions And Answers

More Tutorials









VBA Declaring Variables

Type Hints


Type Hints are heavily discouraged. They exist and are documented here for historical and backward-compatibility reasons. You should use the As [DataType] syntax instead.

Public Sub ExampleDeclaration()
 Dim someInteger% '% Equivalent to "As Integer"
 Dim someLong& '& Equivalent to "As Long"
 Dim someDecimal@ '@ Equivalent to "As Currency"
 Dim someSingle! '! Equivalent to "As Single"
 Dim someDouble# '# Equivalent to "As Double"
 Dim someString$ '$ Equivalent to "As String"
 Dim someLongLong^ '^ Equivalent to "As LongLong" in 64-bit VBA hosts
End Sub

Type hints significantly decrease code readability and encourage a legacy Hungarian Notation which also hinders readability:

Dim strFile$
Dim iFile%

Instead, declare variables closer to their usage and name things for what they're used, not after their type:

Dim path As String
Dim handle As Integer

Type hints can also be used on literals, to enforce a specific type. By default, a numeric literal smaller than 32,768 will be interpreted as an Integer literal, but with a type hint you can control that:

Dim foo 'implicit Variant
foo = 42& ' foo is now a Long
foo = 42# ' foo is now a Double
Debug.Print TypeName(42!) ' prints "Single"

Type hints are usually not needed on literals, because they would be assigned to a variable declared with an explicit type, or implicitly converted to the appropriate type when passed as parameters. Implicit conversions can be avoided using one of the explicit type conversion functions:

'Calls procedure DoSomething and passes a literal 42 as a Long using a type hint
DoSomething 42&
'Calls procedure DoSomething and passes a literal 42 explicitly converted to a Long
DoSomething CLng(42)

String-returning built-in functions


The majority of the built-in functions that handle strings come in two versions: A loosely typed version that returns a Variant, and a strongly typed version (ending with $) that returns a String. Unless you are assigning the return value to a Variant, you should prefer the version that returns a String - otherwise there is an implicit conversion of the return value.

Debug.Print Left(foo, 2) 'Left returns a Variant
Debug.Print Left$(foo, 2) 'Left$ returns a String


Conclusion

In this page (written and validated by ) you learned about VBA Declaring Variables . What's Next? If you are interested in completing VBA tutorial, your next topic will be learning about: VBA Variables.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.