Day 2 VBA Hero – Using the Visual Basic Editor


All of the code that you will need to write, will be done in the Visual Basic Editor.

There are three ways to access it.

  1. You can right on the name of a tab, the choose View Code. (Image 1)
  2. Go to the developper tab and click the Visual Basic button (Image 2)
  3. Use the keyboard shortcut, ALT + F11

Once the Visual Basic Editor is open, you will need to insert a Module (Image 3)

In the editor, there are only two choices to start a script.

Sub or Function

A Sub (or subroutine) is a series of actions that are done in a desired order.
A function is a series of manipulations that you would do to an input to output a desired result.

A subroutine could be something like copying and pasting the same information on 50 different tabs in a workbook.

While a function would be similar to a formula that you would use. The function can also use aspects of a subroutine, but generally, there is a result at the end.

When creating a sub, the naming conventions of the subroutine is:
must start with a letter
No spaces, hyphens, periods, exclamation marks or the characters @, &, $, #
Not case sensitive
Less than 255 characters

When creating a subroutine, you would write it out something like this. (Image 4)

The open and closing of the parentheses, and the “End Sub” are adding automatically.

The code that you would write would be after the line that starts with “Sub” and before the line that starts with “End”.

Verified by MonsterInsights