Visual Basic for Applications/Expressions

This lesson introduces expressions.

Objectives and Skills

edit

Objectives and skills for using expressions include:

  • Customizing a message box to solicit input from the user
  • Using the InputBox function

Readings

edit
  1. Wikipedia: Expression (computer science)
  2. Wikipedia: Statement (computer science)
  3. Wikipedia: Order of operations
  4. Microsoft: Arithmetic Operators
  5. Microsoft: Operator Precedence

Multimedia

edit

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
  1. Arithmetic / Mathematical Operators
    1. Review ExcelFunctions.net: VBA Operators and Built-In Functions.
    2. Experiment with different arithmetic / mathematical operators to ensure you understand how they work.
  2. Order of Operations
    1. Review MathsIsFun: Order of Operations.
    2. Create a macro that demonstrates the order of operations for VBA operators.
  3. Age Calculations
    1. Create a macro that asks the user how old they are in years, and then calculate and display their approximate age in months, days, hours, and seconds.
  4. Temperature Conversion
    1. Review MathsIsFun: Conversion of Temperature.
    2. Create a macro that asks the user for a Fahrenheit temperature and then calculate and display the corresponding Celsius temperature or ask the user for a Celsius temperature and then calculate and display the corresponding Fahrenheit temperature. Include the input temperature in the output display. For example: 98.6 degrees Fahrenheit is 37 degrees Celsius
  5. Area Calculations
    1. Review MathsIsFun: Area of Plane Shapes.
    2. Create a macro that asks the user for the dimensions of different shapes and then calculate and display the area of the shapes.

See Also

edit

References

edit
  Type classification: this is a lesson resource.