Visual Basic for Applications/Expressions
< Visual Basic for ApplicationsThis lesson introduces expressions.
Objectives and Skills
Objectives and skills for using expressions include:
- Customizing a message box to solicit input from the user
- Using the InputBox function
Readings
- Read Wikipedia: Expression (computer science).
- Read Wikipedia: Statement (computer science).
- Read Wikipedia: Order of operations.
- Read Microsoft: Arithmetic Operators.
- 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
- 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.
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.