Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

The Absolute Minimum

This chapter gave you the nitty-gritty on VBA's variables. You learned what variables are and how to declare them, and you learned the various data types supported by VBA. I finished by showing you how to work with arrays and constants.

You also learned a few things that will help you avoid the most common variable-related errors made by beginning programmers:

  • Declare all your variables.

  • To ensure you declare all your variables, add the Option Explicit statement to the top of all your modules (or get VBA to do it for you automatically).

  • Assign a specific data type to each variable.

  • Put all your variable declarations at the top of each procedure (that is, immediately after the Sub or Function statement).

  • To avoid confusing a variable with a built-in VBA name, use a lowercase first letter in each of your variable names.

  • After a variable has been declared, enter all subsequent instances of the variable entirely in lowercase. After you move the cursor away from the variable name, the Visual Basic Editor should change the case to match what you have in the original declaration. If it doesn't do this, it likely means you spelled the variable name wrong.

Here's a list of chapters where you'll find related information:

  • You often use operators and expressions to assign values to variables. I discuss this in detail in Chapter 4, "Building VBA Expressions."

  • Objects have a separate variable type. I talk about it, as well as about assigning objects to variables, in Chapter 5, "Working with Objects."

  • See Chapter 12, "Interacting with the User," to learn more about the details of the MsgBox statement.

  • + Share This
  • 🔖 Save To Your Account