In this session, we delve deeper into the functionality of the if-else statement in VBA (Visual Basic for Applications). We explore how to manage conditions when an initial condition is not met, providing alternate actions to ensure robust and error-free code execution. Practical examples using variables and message boxes illustrate the concept, making it easy to understand and apply in real-world scenarios.

What will you learn

  • Understand the syntax and structure of the if-else statement in VBA.
  • Differentiate between the if and else clauses.
  • Implement conditional logic to handle both met and unmet conditions.
  • Use message boxes to display outcomes based on conditions.
  • Validate outputs by modifying variable values and re-running the script.

Takeaway notes

  • The if statement executes code when a specified condition is true.
  • The else statement provides an alternative action if the if condition is false.
  • Syntax to remember:

```vba If condition Then ' Code to execute if the condition is true Else ' Code to execute if the condition is not true End If ```

  • If conditions enable better error handling and control flow in scripts.
  • Modifying variables in the script allows re-checking and validating the logic implemented.

Practice questions

  1. Write a VBA script that checks if a number entered by a user is positive, negative, or zero and displays an appropriate message.
  2. Create a subprocedure to compare two string values. If they match, display a message box stating, "Strings match"; otherwise, display "Strings do not match."
  3. Using the if-else statement, write a script that determines if a given year is a leap year or not.
  4. Develop a VBA script that reads a user's age and checks if the user is eligible to vote (age >= 18). Display different messages for eligible and non-eligible users.
  5. Implement a script that checks if a cell in an Excel worksheet is empty or contains a value. Display a message accordingly.
  6. Create a subroutine to compare the lengths of two text strings. Indicate which string is longer or if they are equal in length using message boxes.
  7. Write a script that uses the if-else statement to find out if a given number is even or odd.
  8. Develop a script that reads the temperature value and provides a message indicating whether it is 'Hot', 'Cold', or 'Moderate'.
  9. Write a VBA script that checks a user's input for a valid email format; if true, display "Valid Email," otherwise display "Invalid Email."
  10. Create a subroutine to verify if a given number is within a specified range (e.g., 1 to 100) and display messages for both in-range and out-of-range values.
  11. Write a script to check if a given date is a weekend or a weekday and display the corresponding message.
  12. Develop a script that checks the stock quantity of a product. Display a warning if the stock is below a threshold level.
  13. Implement a script to compare two dates and display which one is earlier or if they are the same.
  14. Write a script that takes user input for a score and then provides grade classification (e.g., A, B, C, etc.) based on specific ranges.
  15. Create a script to calculate and compare the areas of two rectangles and display which one is larger or if they are equal.

Mark Lesson Complete (Mastering the If-Else Statement in VBA)