Visual Basic for Applications/Conditions
Appearance
This lesson introduces conditions.
Objectives and Skills
[edit | edit source]Objectives and skills for conditions include:
- Applying If...Then and If...Then...Else decision structures to control execution of code
- Applying a Select Case decision structure to control execution of code
Readings
[edit | edit source]- Wikipedia: Conditional (computer programming)
- Wikipedia: Switch statement
- Microsoft: Variant Data Type
- Microsoft: Using If...Then...Else Statements
- Microsoft: Using Select Case Statements
Multimedia
[edit | edit source]Examples
[edit | edit source]Sub Conditions()
Const Title = "Conditions"
Dim Value As Variant
Dim Result As Variant
Value = InputBox("Enter a value:", Title)
If Value = "" Then
End
End If
Select Case Value
Case "M", "m":
Result = "Monday"
Case "T", "t":
Result = "Tuesday"
Case "W", "w":
Result = "Wednesday"
Case "H", "h":
Result = "Thursday"
Case "F", "f":
Result = "Friday"
Case Else
Result = "Weekend or Invalid"
End Select
MsgBox "The day you selected is: " & Result, vbOKOnly + vbInformation, Title
End Sub
Activities
[edit | edit source]In these activities you will create macros that use input boxes to obtain user input, variables and conditions to perform calculations, and message boxes to display results. Your macros should include Option Explicit, Dim, InputBox, MsgBox, titles on the dialog boxes, an icon on the message box, and use appropriate data types for your variables.
- Logical Operators
- Review Microsoft: Logical Operators.
- Create a macro that uses different values and conditions to show that in VBA zero is false and anything non-zero is true. Also show that the explicit value of False is 0 and the explicit value of True is -1.
- Age Calculations - If/ElseIf/Else
- Create a macro that asks the user how old they are in years. Then ask the user if they would like to know how old they are in months, days, hours, or seconds. Use an If/ElseIf/Else statement to display their approximate age in the selected time frame.
- Temperature Conversion - If/ElseIf/Else
- Review MathsIsFun: Conversion of Temperature.
- Create a macro that asks the user if they would like to convert Fahrenheit to Celsius or Celsius to Fahrenheit. Use an If/ElseIf/Else statement to determine their selection and then gather the appropriate input and calculate and display the converted temperature.
- Area Calculations - If/ElseIf/Else
- Review MathsIsFun: Area of Plane Shapes.
- Create a macro that asks the user what shape they would like to calculate the area for. Use an If/ElseIf/Else statement to determine their selection and then gather the appropriate input and calculate and display the area of the shape.
- Age Calculations - Select Case
- Create a macro that asks the user how old they are in years. Then ask the user if they would like to know how old they are in months, days, hours, or seconds. Use a Select Case statement to display their approximate age in the selected time frame.
- Temperature Conversion - Select Case
- Review MathsIsFun: Conversion of Temperature.
- Create a macro that asks the user if they would like to convert Fahrenheit to Celsius or Celsius to Fahrenheit. Use a Select Case statement to determine their selection and then gather the appropriate input and calculate and display the converted temperature.
- Area Calculations - Select Case
- Review MathsIsFun: Area of Plane Shapes.
- Create a macro that asks the user what shape they would like to calculate the area for. Use a Select Case statement to determine their selection and then gather the appropriate input and calculate and display the area of the shape.