You are on page 1of 9

The 7 Steps to

Getting Started with


Macros & VBA
Quick Guide to Start Writing Your Own Macros

Jon Acampora
1. The Macro Enabled Workbook (.xlsm)
Excel files that contain macros must be saved as a file type that is compatible with macros.
The most common file type is the Macro-Enabled Workbook, and has the .xlsm extension.
To save a file as a .xlsm, we need to change the file type from the Save As… menu:
1. Go to the File menu and choose Save As… Click Browse to open the Save As window.
2. Choose Excel Macro-Enabled Workbook (.xlsm) from the Save as type list of file
extensions.
3. Name the file and press Save.

You can save existing files or new files as macro enabled workbooks.

ExcelCampus.com 1
2. Enable the Developer Tab in Excel
The Developer tab contains a set of buttons and tools that will help us when writing macros
and developing VBA applications.
The Visual Basic button opens the VB Editor, which is the tool we use to write macros. The
keyboard shortcut to open the VB Editor is Alt+F11

If you do not see the Developer tab in your Excel Ribbon, it is very easy to enable it:
1. Right-click any of the tabs in the ribbon and choose Customize the Ribbon…
2. Click the Developer checkbox on the Main Tabs menu, and press OK.

The Developer tab will now appear every time you open Excel.
ExcelCampus.com 2
3. Write a Macro in the VB Editor
Macros can be written in Code Modules inside the VB Editor. There are other objects or places
we can write macros, but the Code Module is the most common place to start.
A code module is really just a blank page that stores the macro code. The VB Editor displays
VBA code syntax and markup by changing the color of the text and capitalizing words that it
recognizes.
To insert a new Code Module:
1. Open the VB Editor
2. Select the file you are working on from the Projects window on the left side (Ctrl+R)
3. Go to the Insert menu and select Module (Alt,i,m)
4. A module will be added to the Modules folder of the file in the Projects window.
5. The code window (text editor) will appear on the right side. Double-click the module in
the Project window if you do not see the code window.

ExcelCampus.com 3
4. The Excel Object Model
Everything we interact with in Excel is considered an Object in VBA. This includes workbooks,
worksheets, cells, ranges, charts, pivot tables, shapes, tables, etc.

We program these objects with Properties and Methods (Actions).

ExcelCampus.com 4
5. Step Through and Run the Macro
When writing macros, it is a good idea to continually test the code by stepping through each
line.
To Step Through a macro:
1. Place the text cursor anywhere inside the macro you want to run.
2. Press F8 on the keyboard (Debug > Step Into).
3. The first line of macro will be highlighted in yellow.
4. Continue to press F8 to step through and run each line of code. The line of code that is
currently highlighted will run after you press F8 again.
5. When you get to End Sub line, press F8 again to stop running the code. You can also
press the Stop button on the toolbar in the VB Editor.
Alternatively, you can run the entire macro by pressing F5 on the keyboard, or the Play button
in the toolbar of the VB Editor.

There are a few important things to note when running or stepping through macros.
1. The code runs from top down, like reading a page in a book.
2. Only one line of code is run at a time.
3. VBA needs context or it assumes you are referring to the ActiveWorkbook and
ActiveWorksheet. Make sure you activate (select) the right workbook or worksheet
when testing your code, or include these references in the code.

ExcelCampus.com 5
6. The Object Hierarchy
The objects, properties, and methods are organized in a hierarchy. The hierarchy starts at the
Application level.

The hierarchy is used to store the various object collections in VBA.


 All open workbooks are a member of the Application (Excel application).
 The workbook contains a collection of worksheets.
 Each worksheet contains objects that are members of the worksheet object. This
includes: cells, charts, pivot tables, slicers, tables, shapes, and a lot more.

Remember that VBA needs context. If you do not specify which workbook or worksheet to run
the code on, it will assume you are referring to the ActiveWorkbook and ActiveWorksheet.
This can lead to errors or disaster.

ExcelCampus.com 6
7. The Macro Recorder & Beyond
The macro recorder is a fantastic tool built into Excel. It produces the VBA code for all the
actions we take in Excel.
We can use the macro recorder to learn the object, property and method references that we
need for a macro. It’s a fast way to get snippets of code.
To record a macro:
1. Press the Record Macro button on the Developer tab of the Ribbon in Excel.
2. Give the macro a name and press OK.
3. Record some actions like adding a new sheet, changing a cell’s font color to red, writing
a formula, etc.
4. Press the Stop Recording button on the Developer tab.
5. Open the VB Editor (Alt+F11).
6. A new code module will be inserted in the Modules folder of the workbook you
recorded the macro in. Double-click it to view the recorded macro.

The macro recorder can help us learn how to reference and modify the objects in Excel.
However, it does have some limitations. It cannot create more advanced coding techniques
like loops, if statements, arrays, message boxes, userforms, etc.
The true power of automating Excel is unleashed with these coding techniques in VBA. One
of the most powerful and commonly used coding principles is the For-Next Loop. Loops allow
us to automate boring repetitive tasks, and they can save us hours of time.

ExcelCampus.com 7
And finally… Practice, Practice, Practice!
I hope this guide and training helps get you started
writing macros. VBA is an amazing tool that can save
you tons of time, and also take your career to the next
level.
With any new skill, it’s best to practice as much as you
can. VBA might look scary at first, but I know you can
do it. Once you start writing your first macros and see
the power of automation, you will quickly get hooked
like I did.
Thanks again for joining me on the journey to learn
Excel & VBA. I will be back soon with more info to help
you become an Excel superstar. 

Happy Learning!

Jon Acampora
Excel Campus
excelcampus.com

ExcelCampus.com 8

You might also like