Visual Basic for Applications/Variables

This lesson introduces variables.

Objectives and Skills

edit

Objectives and skills for using variables include:

  • Declaring variables by using the appropriate data types
  • Making variables available to procedures in all modules of an application
  • Performing actions with built-in and user-defined constants

Readings

edit
  1. Wikipedia: Variable (computer science)
  2. Wikipedia: Constant (computer programming)
  3. Wikipedia: Data type
  4. Wikipedia: Type conversion
  5. Wikipedia: Scope (computer science)
  6. Microsoft: Declaring Variables
  7. Microsoft: Option Explicit
  8. Microsoft: InputBox Function
  9. Microsoft: Let Statement

Multimedia

edit
  1. YouTube: InputBox

Examples

edit
'This macro accepts user input and then displays double the value entered.

Option Explicit

Sub DoubleValue()
    Const Title = "Double Value"
    Dim Value As Single
    Dim Result As Single
    
    Value = InputBox("Enter a value:", Title)
    Result = Value * 2
    MsgBox Value & " * 2 = " & Result, vbOKOnly + vbInformation, Title
End Sub

Activities

edit

See Also

edit

References

edit
  Type classification: this is a lesson resource.