Visual Basic for Applications/Expressions
Appearance
This lesson introduces expressions.
Objectives and Skills
[edit | edit source]Objectives and skills for using expressions include:
- Customizing a message box to solicit input from the user
- Using the InputBox function
Readings
[edit | edit source]- Wikipedia: Expression (computer science)
- Wikipedia: Statement (computer science)
- Wikipedia: Order of operations
- Microsoft: Arithmetic Operators
- Microsoft: Operator Precedence
Multimedia
[edit | edit source]Examples
[edit | edit source]'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 | edit source]- Arithmetic / Mathematical Operators
- Review ExcelFunctions.net: VBA Operators and Built-In Functions.
- Experiment with different arithmetic / mathematical operators to ensure you understand how they work.
- Order of Operations
- Review MathsIsFun: Order of Operations.
- Create a macro that demonstrates the order of operations for VBA operators.
- Age Calculations
- 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.
- Temperature Conversion
- Review MathsIsFun: Conversion of Temperature.
- 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
- Area Calculations
- Review MathsIsFun: Area of Plane Shapes.
- Create a macro that asks the user for the dimensions of different shapes and then calculate and display the area of the shapes.