Understanding Subprocedures in VBA for Excel

In this video, we delve into subprocedures in VBA (Visual Basic for Applications) within Excel. You will learn the differences between functions and subprocedures, particularly how subprocedures do not return values. The video demonstrates the process of creating and calling subprocedures using an example of calculating the area of a rectangle, displaying the result in a message box rather than returning a value to Excel cells.

What will you learn

  • The fundamental difference between functions and subprocedures in VBA.
  • How to create and use subprocedures in VBA for Excel.
  • The process of passing arguments to subprocedures.
  • How to use a subprocedure within a function to perform calculations.
  • Displaying results in a message box using subprocedures.

Takeaway notes

  • Subprocedures do not return values, while functions may or may not.
  • You need to call a subprocedure within a function to execute it in Excel.
  • Subprocedures can take arguments, process them, and then perform actions such as displaying a message box.
  • It's good practice to insert a new module for writing subprocedures and functions in VBA.

Practice questions

  1. Describe the difference between functions and subprocedures in VBA.
  2. How do you insert a new module in the VBA editor?
  3. Write a VBA subprocedure that takes two numbers as input and displays their sum in a message box.
  4. How do you pass arguments to a subprocedure in VBA?
  5. Create a function that calls a subprocedure to calculate and display the perimeter of a rectangle.
  6. Explain what happens to the return value when a subprocedure is called from a function in Excel VBA.
  7. Write a subprocedure that takes three numbers and displays their average in a message box.
  8. How would you modify a VBA function to use a subprocedure for its core calculation?
  9. In VBA, what keyword is used to declare the start of a subprocedure?
  10. What kind of actions can a subprocedure perform in VBA?
  11. Write a subprocedure that multiplies two numbers and displays the result in a message box without returning a value to the function that called it.
  12. Explain why the cell value becomes 0 when using a subprocedure instead of a function to perform a calculation.
  13. How do you call a subprocedure from within a VBA function?
  14. Create a VBA subprocedure that displays the message "Hello, World!" in a message box.
  15. Discuss the benefits of using subprocedures for code reusability and organization in larger VBA projects.

Mark Lesson Complete (Understanding Subprocedures in VBA for Excel)