Visual Basic for Applications/Expressions

< Visual Basic for Applications

This lesson introduces expressions.

Objectives and Skills

Objectives and skills for using expressions include:

Readings

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

Examples

'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

  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.

References

    This article is issued from Wikiversity - version of the Thursday, December 03, 2015. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.