Visual Basic for Applications/Conditions

< Visual Basic for Applications

This lesson introduces conditions.

Objectives and Skills

Objectives and skills for conditions include:

Readings

  1. Read Wikipedia: Conditional (computer programming).
  2. Read Wikipedia: Switch statement.
  3. Read Microsoft: Variant Data Type.
  4. Read Microsoft: Using If...Then...Else Statements.
  5. Read Microsoft: Using Select Case Statements.

Examples

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

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.

  1. Logical Operators
    1. Review Microsoft: Logical Operators.
    2. 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.
  2. Age Calculations - If/ElseIf/Else
    1. 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.
  3. Temperature Conversion - If/ElseIf/Else
    1. Review MathsIsFun: Conversion of Temperature.
    2. 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.
  4. Area Calculations - If/ElseIf/Else
    1. Review MathsIsFun: Area of Plane Shapes.
    2. 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.
  5. Age Calculations - Select Case
    1. 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.
  6. Temperature Conversion - Select Case
    1. Review MathsIsFun: Conversion of Temperature.
    2. 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.
  7. Area Calculations - Select Case
    1. Review MathsIsFun: Area of Plane Shapes.
    2. 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.

See Also

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.