"Deadline-Beating Blueprints Trusted by Intermediate and Advanced Excel Users"

How to find the code of a VBA macro in a workbook

Are you asking where the macros in a workbook are?

CTRL + F11, show the VBA interface

Well, you should know that Excel stores VBA stuff: macros, modules, user forms, etc. as well as spreadsheet data inside workbooks.

How does this work? Here’s a quick explanation…

Excel files contain data and vba code

To access your macros VBA code you need to open the workbook compartment that contains VBA data, this is the VBA interface.

There you will find the macros VBA code (procedures) in modules, see the picture below…

Sub procedures

Launch the VBA editor this way…

Excel 2007/2010 users…

The fastest way to show the VBA interface is by using the shortcut ALT+F11, type it when you are in the workbook environment.

You can also use the Ribbon. Excel 2007/2010 brings the “Developer” tab hidden by default. Activate it this way…

If you are using Excel 2007 go to the Microsoft Office button>Excel Options>Popular>”Show Developer tab in the Ribbon”>OK

If you are using Excel 2010 go to the Microsoft Office button>Excel Options>Customize Ribbon, and then activate the “Developer” tab, see the picture below…

VBA interface, Excel options, customize ribbon

Once the Developer tab is shown in the Excel ribbon, activate it and click on the “Visual Basic” toolbar.

Another handy way to show the VBA editor is by right clicking over any sheet tab of your workbook and choosing “View code”.

You can also go directly to any given macro by going to the Ribbon: Developer>Code>Macros>Select your macro>Edit. See the picture below…

Select the macro and click edit to show the VBA code

Excel 97-2003 users…

Click ALT+F11 or go to Tools>Macro>Visual Basic Editor.

The same right click technique applies to Excel 2003, just right click over any sheet tab of your workbook and choose “View code”.

You can also go directly to any given macro by going to the Tools>Macro>Macros>Select your macro>Edit.

How to record a macro in Excel that works dynamically on different array sizes

You want to record dynamic Excel Macros that work on variable-size arrays, right?

Maybe you recorded/created an Excel VBA Macro and worked awesome this time, but you are wondering if it will work the same way the next time.

In just a moment you are about to learn how to record a dynamic Excel Macro using relative references and some mouse techniques.

If you still don’t know how to record a macro in Excel don’t be afraid because recording Excel Macros is simple!

Here’s how to do it…

  1. How to record a macro in Excel that works dynamically on different array sizes and layouts
  2. How to use a dynamic macro you recorded in Excel

How to record a macro in Excel that works dynamically on different array sizes and layouts

  1. Put your cursor at the starting position of your raw data (Don’t include earlier mouse movements in your Macro)
  2. Launch the Record Macro dialog
  3. Assign a shortcut to your Macro (so you can run it repeatedly easily)
  4. Click Ok on the Record Macro dialog
  5. Use relative references (On Excel 2003 do it this way: click the Relative Reference button on the Stop Recording toolbar. On Excel 2007 do it this way: go to View>Macros> Macros>Use Relative References. On Excel 2016 go to Developer>Code>Use Relative References). Do this before you perform any movement of the Excel cursor around your spreadsheet
  6. Record one instance of the dynamic Excel Macro (if you have one thousands blocks of data, record just one)
  7. Stop the Macro when your mouse is at the starting position of your next block of data (corresponding position to the location you started on step 1 of this tutorial)

Congratulations! You have recorded a dynamic Excel Macro.

Now it’s time to use the dynamic macro you just recorded.

How to use a dynamic macro you recorded in Excel

Use your dynamic Macro this way:

Put your cursor at the starting position and then press the Macro shortcut. If you want to process N blocks of data, press CTRL + YOURKEY N times, or keep your shortcut pressed and the Excel Macro will be repeated effortlessly.

For instance, you can use this dynamic macro recording technique to convert raw data like this one:

How to record macro in Excel dynamically

 

Into table layout like this one:

How to record a macro in Excel that works on different array sizes

Your recorded Macros will work for one row or for one million rows, for one column or one hundred columns.

Want to go to the next level?

Then you need to learn how to write Excel Macros from scratch.

Quick tip to make your recorded Excel macros super fast

Here’s the tip.

Turn off screen updating.

Doing this is like jumping to the desired movie scene instead of watching a chain of quick-motion sequences before you watch it.

When you do this, your macro will not spend computing resources to update the screen at every action it performs.

  • Shifting sheets (this does not occur if you implement tip 2 mentioned above)
  • Range selections  (this does not occur if you implement tip 2 mentioned above)
  • Chart creation
  • Table creation
  • and other outputs

How do you do that?

It’s very simple…

Write this instruction at the beginning of your macro (after the “Public Sub ()” line)…

“Application.ScreenUpdating = False” (without quotation marks)

And this one at the end of your macro (before the “End Sub” line)…

“Application.ScreenUpdating = True” (without quotation marks)