4 Mistakes You Can Avoid when Programming Excel Macros with VBA

  • Make them as short as possible.
  • Make them as descriptive as possible.
  • Preface them with variable type (boolean, integer, etc…).
  • Remember to use the right scope (see below).

Here’s a sample screenshot from a program that I use often to make WMIC Windows calls from Excel to gather PC information.computer science computer science computer science computer science computer science

When you want to use the variables inside of any function inside of the module or object (I will explain this below), then you need to declare it as a “public” variable by prefacing the declaration with Public. Otherwise, variables get declared by prefacing them with the word Dim.

As you can see, if the variable is an integer, it’s prefaced with int. If it’s a string, then str. This helps later on while you’re programming because you’ll always know what type of data the variable holds, just by glancing at the name. You’ll also notice that if there’s something like a string that holds a computer name, then the variable is called strComputerName.

Avoid making very convoluted or confusing variable names that only you understand. Make it easier for another programmer to come behind you and understand what it all means!

Prev3 of 9Next

Leave a Reply

Your email address will not be published. Required fields are marked *