Jump to content

Visual Basic for Applications/Conditions

From Wikiversity

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]
  1. Wikipedia: Conditional (computer programming)
  2. Wikipedia: Switch statement
  3. Microsoft: Variant Data Type
  4. Microsoft: Using If...Then...Else Statements
  5. 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.

  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

[edit | edit source]

References

[edit | edit source]
Type classification: this is a lesson resource.