What would James Bond have in his Personal Macro Workbook?

Posted on November 18th, 2013 in Posts by Jeff , VBA Macros - 23 comments

Howdy folks. Jeff Weir here. The subject of today’s post is “What would James Bond have in his Personal Macro Workbook”. The Personal Macro Workbook is a secret place where you can save frequently used macros, automation snippets and code modules so that you can call them from any file and save tons of time, not to mention look dashingly good in-front of your boss. In this article, we’ll take a look at how to set up a personal macro workbook and use it.

The other day, undercover Excel secret agent KV came up a great list of interview questions in response to Chandoo’s great article What are best Excel interview questions?

I particularly liked this one:
Do you have a Personal Macro Workbook setup ? If yes, what are the most common macros you use in your daily work?

Secret Agent KV obviously has some purpose-built gadgets stored in his Personal Macro Workbook that he employs to kill inefficiencies. And he obviously also knows the two top-secret reasons to keep code in a Personal Macro Workbook:

  1. To keep it safe from international spy rings.
  2. So you can whip it out whenever you are cornered by any evil-looking spreadsheet.

While that first point is important, that second is the clincher: code stored in a personal workbook can be unleashed with a simple judo-chop to the throat. Err, I mean a simple Alt + F8 to the keyboard:
Bond's Personal Macro Workbook

And the best thing of all: this top secret code can be recycled over and over again, just like a James Bond plot!

But I don’t know how to write code… I hear you say. (I’ve bugged your office). Well…does James Bond build his own top-secret gadgets? No, of course not. He has Q to do it for him:
James_Bond's_Qs

Okay, okay…so you don’t have Q at your disposal. But you do have G:
google-iam-feeling-lucky-search-plug-in for mozilla firefox

Google. Because the secret code for world Excel domination has been leaked all over the internet, by the likes of secret agent KV and 10,000 other spooks just like him. Many strains of this code are highly lethal to just about any kind of inefficiency you could ever think of. You just need to cut and paste it into your Personal Macro Workbook and you’re on your way to world Excel Domination.

So in the international war on spreadsheet terror, KV’s on to something. Which is why I asked him if to digital pen to digital paper, and share some more of his ill-gotten top secret code with us here on ExcelWikiLeaks.

Unfortunately for us, he said no. So I told him that unless he complies by this time tomorrow, I’ll publish his real name. So he said yes. (Khushnood Viccaji is obviously a man with something to hide. But don’t worry, Khushnood …your secret identity is safe with me.) So tune in tomorrow to see secret spy code in action.

Meanwhile, I’ll set the scene by showing you where and how to stash your to secret code. And after that, I’ll point you to some code I’ve only just completed that could well save you hours if you use Pivots a lot. So read right to the end, good spooks.

What exactly is a Personal Macro Workbook?

Often on the internet you’ll find workbooks that you can download that contain useful inefficiency-killing code. But those macros are firmly attached to the workbooks they live in. So as long as you have those workbooks open, you can use those macros. But as soon as you close those workbooks, those macros are no longer available.

But just as secret agents have briefcases with false bottoms (and possibly girls with false body-parts), Excel has a hidden workbook – the Personal Macro Workbook – that gets opened automatically any time you start Excel.

So just like James Bond’s hidden gadgets, any code you stash in there is constantly at your command, just waiting to be whipped out and triggered at the slightest provocation. (Which reminds me of another aspect of Mr Bond, but we’ll keep this family friendly for now, eh?)

How do we set up our Personal Macro Workbook?

Note that the Personal Macro Workbook doesn’t exist until we instruct Excel to create it. But that’s simple. If you’ve got Excel 2010, here’s how. (If you’ve got a different version, you may need to do some secret squirrel industrial espionage via Google to get the secret plans for your particular version)

Step One: Make sure the Developer Ribbon has been enabled.

This isn’t strictly necessary, but we’ll go ahead and do it anyway. Have a look at the top of the workbook. Do you have a menu option ‘Developer’ showing?
Developer Ribbon from Home tab

Yes? Great.

No? No problem, let’s sort that now. Click File, and select the Options option:

File_Options

Now click Customize Ribbon, and then check the Developer checkbox on the right hand side, then press OK:

Customize the Ribbon_Developer

Cool, now that Developer option should show in the ribbon.

Step two: Record a ‘dummy’ macro into your Personal Macro Workbook

To create our Personal Macro Workbook, we simply record a macro and tell Excel to put the macro inside it. Excel then realizes “Well, duh…I better create a Personal Macro Workbook first, because it doesn’t actually exist yet”.

To do this, click on the Developer option in the ribbon, which brings up a whole heap of fancy looking options. (Don’t worry, even James Bond doesn’t know what most of these do).

Developer Ribbon selected

Now click the Record Macro option that shows in the left hand side of the Developer ribbon:

Macros tab_Record Macro

Note that you could also click the exact same icon that you’ll find down the bottom left hand corner of the spreadsheet:

Bottom Bar_Record Macro_Oval

Either way, this brinks up the Record Macro dialog box:

Record Macro dialog box_This Workbook oval

By default, Excel tells you that it will store the macro it’s about to record in This Workbook. But that’s not what we want this time. Instead, we want Excel to store the macro in our Personal Macro Workbook, which will force Excel to create the Personal Macro Workbook.

So change that “Store Macro In” drop-down to this:

Record Macro dialog box_Personal Macro Workbook oval

Now go ahead and push OK.

Now…I don’t want you to panic or freak out or anything… so promise me you’ll listen very calmly to what I’m about to say. Promise? Okay, good. The Macro Recorder is now recording everything you do. So try not to make any sudden moves or pull any funny faces. Don’t pick your nose. And please, don’t make any loud body noises. Because it’s all going down on tape. Just act normal. (Unless of course you routinely pull funny faces while picking your nose to the tune of loud bodily noises. In which case, give me you best abnormal.) That’s it.

Okay…now we want to carefully stop recording. Why? Because the purpose of this exercise is not to actually create magnificent code, but rather to create a secret compartment within Excel in which to store magnificent code. And merely recording a completely blank macro like we just did is enough to get Excel to create our top secret Personal Macro Workbook.

To shut this thing down, I need you to s-l-o-w-l-y reach into your spy satchel and pull out the red-handled, thin-nosed wire cutters. Now, c-a-r-e-f-u-l-l-y unscrew the bomb housing, and locate the detonator. See the blue wire sticking out of the detonator? C-a-r-e-f-u-l-l-y cut it before the timer goes to Zero.

No, wait a minute…I think it’s the red wire.

NO, WAIT…THAT’S NOT IT EITHER. Umm…ahh…err….Ah…quick…Jeff…think…argggh…Oh No…Mummy!

WAIT, I REMEMBER NOW! To shut this thing down, push the STOP RECORDING buttons in the ribbon:

Macros tab_Stop Recording

Alternately, you can click the Stop Recording icon that you’ll find down the bottom left hand corner of the spreadsheet:

Bottom Bar_Stop Recording_Oval

Phew, that was close. Sorry about that. Hey, well done, you! Believe it or not, just by setting the macro recorder to Record and then clicking Stop forced Excel to create your very own Personal Macro Workbook. Who said giving birth was hard, eh! So where is this new baby? Well, duh…she’s hidden. I mean, really! Are you fit to be a parent, with an attention span like that?

Alright, alright, if you really want me to prove this immaculate conception, then click View from the top of the ribbon, and then click Unhide:

Unhide Ribbon

BEHOLD! A secret hidden workbook called PERSONAL!. Just waiting to be filled with your secret stash of code.

Unhide

Isn’t she a beautiful baby? Look, she has your father’s eyes! (Don’t bother pushing OK to unhide her…this was just to prove that she exits.)

Step three: Put some re-usable code in there

The easiest way to get code into your Personal Macro Workbook is via something called the Visual Basic Editor, or VBE for short. You can open the VBE by pushing Alt + F11 from Excel. (Pushing Alt + F11 again takes you back to Excel). Go ahead, give it a try. You’ll see something like this:

VBE_NoWindows

Scary, isn’t it. It doesn’t look anything like the Excel that we know and love. In fact, it’s about as attractive as a villain from a Bond film. But don’t worry, we’ll defeat this bad boy together.

(Note that depending on your settings, you might see a few more windows than what the above screenshot shows. So just ignore any of the below steps that aren’t relevant.)

The first thing we want to do is to bring up something called the Project Explorer window, which is a tree-map view of all the different workbooks and Add-Ins that you currently have loaded. To make it visible, select Project Explorer from the View tab:

VBE_EnableProjectExplorer

…which will bring up this:

VBE_ProjectExplorer_AddIns

And look…we can already see our new baby listed on the side: Personal.XLSB. This being a spy flick and all, Excel gave our new Personal Macro Workbook the code name XLSB. (Note that if your version of Excel is a little older, you will see Personal.XLS instead. Basically you have the Sean Connery bond, and not the Danial Craig revision. But they are both deadly.)

What we need to do is expand that Personal.XLSB node by clicking on the + sign to the left of it. Here’s what the first level looks like:

VBE_ProjectExplorer_Expanded1

So now we see a folder called Microsoft Excel Objects and another one called Modules. The Microsoft Excel Objects node expands out to show us two further nodes called Sheet1 and ThisWorkbook:

VBE_ProjectExplorer_Expanded2

…but we don’t use these, so just ignore them. What we want is to expand the Modules node:

VBE_ProjectExplorer_Expanded3

Now we just need to double-click on that Module1 thing to bring up the Code Window associated with this module. You might already see a code window, or even a whole bunch of them floating on top of each other. Double-clicking on a particular Module will bring the applicable code window to the top.

VBE_ProjectExplorer_Macro1

And look, there’s that Macro we recorded earlier. That’s not so scary is it…now we know where code turns up when recorded to the Personal Macro Workbook – in a Module. Any time you record a new macro, Excel sticks it in a Module. If you’ve already been recording Macros during your current Excel session, then Excel sticks it in the same Module. If you start a new session (e.g. close down Excel, then restart it and record another Macro) then Excel would put the code you record in a new module. And any time you put new code into a Module – either by using the Macro Recorder or by cutting and pasting code directly in to a Module, then when you close Excel it will prompt you if you want to save changes to your Personal Macro Workbook.

It doesn’t really matter which of the Modules you store your code in in that Personal Macro Workbook – just so long as each routine has a different name. In fact, you can have just one module in there if you like that contains hundreds of different routines. I have several modules, that I’ve given names like modPivotTables and modText and modCharts, with each module containing routines related to the name of the module. I find this makes it easier to track down where a particular routine is stored, in case I want to make changes to it in the future. You can rename Modules by bringing up the Properties window. To do this, select view from the Toolbar, and then click Properties Window:

VBE_ProjectExplorer_EnableProperties

…which will bring up a new window that will probably sit right below the Project Explorer window by default:
VBE_ProjectExplorer_ChangeName

If it’s floating somewhere else, just double-click it and it will then ‘dock’ below the Project Explorer window as shown above. To change the name of a Module, just type the new name where I’ve indicated above.

So now we know where to copy new code, if we want to store it there for reuse. Sweet! So let’s delete that code, and put something useful in there right away, shall we?


So what other kinds of things might you want to put in there?

Well, efficiency comes in all shapes and sizes. I’ve got macros in there that are just a few lines long, and that save me a mere two or three mouse-clicks, like this one:


Sub ClearFormats()
Selection.ClearFormats
End Sub

All that does, is clear the formats from the selected range. In fact, it does exactly the same thing as this does:ClearFormats

...the only difference is that I assign this to a keyboard shortcut of [Ctrl] + [C] so that I don't even have to take my hands off the keyboard to launch it. (Sure, I could use Excel's shortcut of [Alt]+[H]+[E]+[F]. But that's more random digits than my memory or fingers can usually handle.)

Assigning a keyboard shortcut to launch a Macro is easy. First, we switch from the VBE back to Excel using [Alt]+[F11], and then we push [Alt] + [F8] to bring up the Macro Dialog Box:
Macro Dialog Box_ClearFormats

Then we click the Options button on the bottom right, which brings up the Macro Options dialog box, where we can enter in the key combination we want to use:

Macro Options

I usually use a combination with [Shift] in it, because this stops me overwriting any of Excel's existing keyboard shortcuts.

Can I put clever home-made Worksheet Functions in there too?

You bet. I often store custom-made functions - called User Defined Functions - in my Personal Macro Workbook that can be used within a formula in a cell, without having to even launch the Run Macro dialog box.

For example, say you have some delimited text in a column like so:
Extract Formula

Let's say you wanted to extract the 3rd item in that list, but for some reason can't use Text-To-Columns because you don't want to blow your Excel Ninja cover. You'd need a pretty complicated formula to do this. Probably something like this:
=MID(A2,FIND("|",SUBSTITUTE(A2,";","|",2))+1,FIND("|", SUBSTITUTE(A2,";","|",3))-FIND("|",SUBSTITUTE(A2,";","|",2))-1)

That formula is a pain to remember how to put together, not to mention a drag to write. So instead, you could just store the following function in your Personal Macro Workbook:


Function SplitText(str As String, strDelimiter As String, lngOccurance As Long)
Dim varArray As Variant
varArray = Split(Expression:=str, delimiter:=strDelimiter)
SplitText = varArray(lngOccurance - 1)
End Function

Then you could use it in any workbook by using this formula:
=PERSONAL.XLSB!SplitText(A2,";",3)
...for exactly the same effect. But with much less effort.

Note that we have to prefix the formula with PERSONAL.XSLB! so that Excel knows where to find the code behind this puppy.

If you send the workbook to someone else that doesn't have the same Macro in their Personal Macro Workbook, then they will just get a #NAME? Error. To avoid this, you could send them the Macro itself and point them to this article. Or you could copy the code from your Personal Macro Workbook and paste it into a Code Module in the Workbook you are sending. To do this, select the workbook of interest in the VBE, then click INSERT and select Module from the menu, as shown below.

VBE_InsertModule

This will put a blank Code Module in the workbook itself, and then you can copy the in the workbook that you are sending out, and then copy the macro from your Personal Macro Workbook into that empty Module:

VBE_WorkbookCodeModule

...and then ditch the =PERSONAL.XLSB bit in your formula:
=ExtractElement(A2,3,";")

Those are fairly trivial, aren't they? What about a really intricate gadget?

At the other extreme, I've got macros in there that I've worked on for months, that save me hours if not days of work. And some let me do the downright impossible.

Here's an example: Imagine you've got a list of 1000 Top Secret Passwords (left) and a PivotTable of 20,000 Top Secret Passwords (right). And imagine that in order to save the world, you need to filter the PivotTable on the right so that it exactly matches the list on the left. Quickly.

Top Secret Passwords

How long do you think it's going to take you to filter that pivot using this:

PivotFilter

Hours? Days? What if the fate of the free world depends on you getting it right? Worried you might make mistakes? Would you even notice? Would you bother checking? How will you even do this...as that PivotFilter Dialog box says, Not All Items Showing! So we can't even see all items to filter them. Goodbye free world!

Enter my FilterPivot macro, that lives in my Personal Macro Workbook. It's just itching for a fight like this one. Let's judo-chop the keyboard with Alt + F8, bring up the Run Macro dialog box, and see it in action, shall we?

Macro Dialog Box_FilterPivot

Look, there it is. All I need to do now is select it, push Run, and follow the instructions:

FilterPivot_FilterItems

*BING*...Done! See - the PivotTable on the right is filtered so that it exactly matches the list on the left.

Bing_Done

How's that. Neat, eh! This code is really, really fast under a range of different scenarios. It works out whether it's quickest to:

  • make all items visible in the pivot, and then hide just the PivotItems that don't match the filter terms; or
  • hide all but one items in the PivotField, and unhide just the PivotItems that do match the filter terms

If you've got a PivotTable with 20,000 items in it, then here's how long it takes:

  • Filter on 100 search terms: 7 seconds
  • Filter on 10,000 search terms: 1 minute, 30 seconds
  • Filter on 19,900 search terms: 5 seconds

Want one of your very own? Go and visit my recent post Filtering Pivots Based On External Ranges over at the Daily Dose of Excel blog, and cut and paste away. Note that this code requires Excel 2010 or later...and you want to scroll to the end of the acticle and look for the code under the heading Faster Approach. If you've got 2007 or earlier, all is not lost: look for the code listed under the Slower Approach heading. It's not as fast as the times I've posted above, but still a heck of a lot faster than doing it my hand.

That's the beauty of the web - there's weapons-grade code just waiting for you to steal. So again, you don't have to know how to write code in order to highly leverage VBA. All you need to know is how to Google, Cut, and Paste. Heck, even Austin Powers can do that without screwing it up!

That's more than enough for today. Tune in tomorrow, and we'll see what kind of time-savers Secret Agent KV has in his Personal Macro Workbook.

And if you know of amazing routines published on the web somewhere that are Personal Macro Workbook ready, then let us know about them in the comments below. But only if these routines are ready to go, and are the best of the best. Like the code at these two links:

http://peltiertech.com/WordPress/label-last-point-for-excel-2007/
http://datapigtechnologies.com/blog/index.php/auto-format-pivottables-to-match-source-data/

About the Author.

Jeff Weir – a local of Galactic North up there in Windy Wellington, New Zealand – is more volatile than INDIRECT and more random than RAND. In fact, his state of mind can be pretty much summed up by this:

=NOT(EVEN(PROPER(OR(RIGHT(TODAY())))))

That’s right, pure #VALUE!

Find out more at http:www.heavydutydecisions.co.nz

Your email address is safe with us. Our policies

Written by Jeff Weir
Tags: , , , , , , ,
Home: Chandoo.org Main Page
? Doubt: Ask an Excel Question

23 Responses to “What would James Bond have in his Personal Macro Workbook?”

  1. Khushnood Viccaji (KV) says:

    At last, this post has seen daylight Jeff ;)

  2. Khushnood Viccaji (KV) says:

    I may be wrong about this, but if the PERSONAL MACRO WORKBOOK is being created for the first time, its file extension may default to .xlsm (Excel Macro-enabled worksheet), rather than .xlsb (Excel Binary worksheet).
    This depends on the default file format selected in the ‘Save’ section of Excel Options setting.

    The difference between these formats was discussed in another thread on this site.
    http://chandoo.org/wp/2013/09/29/i-said-your-spreadsheet-is-really-fat-not-real-phat/

  3. Jeff Weir says:

    Ah. It must default to whatever default type of file format you’ve instructed Excel to use for new workbooks. I’ll make a note above. Thanks KV

    • Khushnood Viccaji (KV) says:

      Correct Jeff… but if the default format is set as .xlsx, then I suppose the PMW would be saved as a .xlsm file.
      The .xlsb file format seems to be a well-hidden, non-default format.

  4. Umang says:

    Great Read !! Delight..

  5. SamJones says:

    Thanks for this – I love Macros!

    But having a personal excel file is only half the battle… you should take it to the next level – the real “win” comes from turning that macro into a button and allocating it a cute picture.

    Boom!

    Game Changer!

    • Khushnood Viccaji (KV) says:

      Right Sam, that’s what I’ve mentioned in the other post yesterday.

      In Excel 2007 and later, the QAT can be customised to run your macros with a single-click of the mouse, or 2 / 3 keyboard strokes :)

      • Bryan says:

        Nothing beats clicking on the smiley face in the QAT, or my second favorite shortcut: Alt + 07. I get so used to it at work that I get home and try it and Excel just beeps at me and it takes a while to figure out what’s gone on.

  6. Bryan says:

    I’ve got just over 4,000 lines of code in my Personal.xlsb, including 168 procedures, 2 forms, 1 class, and 20 modules!

    Most of the most regularly-used functions are very work-specific for me. One takes a range of selected cells and makes a search-friendly string that works with our database interface. Another formats the report exports to look exactly how I like them. One takes a selected row of data and copies boilerplate text to the clipboard for responding to a specific type of email (or if more than one row is selected, creates a table of data to paste into the email). One opens an input form (one of the boxes) and places data next to each of the codes entered.

    Probably 80% of my code is part of the “code library”, so whenever I create something new I don’t have to recreate the wheel.

    One thing, though: I never use my Personal.xlsb to house UDFs, because I’m too lazy to type =Personal.xlsb!UDF(whatever) every time. Instead, I created a .xla add-in that calls the functions stored in my Personal.xlsb (through Application.Run “Personal.xlsb!UDF, whatever); this way, I can skip the “Personal.xlsb!” part of the formula when I use it in an Excel sheet.

  7. hasan says:

    hi very thank you for your message

  8. saad says:

    many thanks sir .

  9. Marie99 says:

    Nice post.

  10. Manjunath says:

    Hi, Great Article…Excellent introduction to personal macro workbook feature.
    But the title of the article was hinting that we’ll get to know some important functions that can be embedded into this..
    Please give some important VBE codes that may be useful for a average excel user..

  11. jina says:

    Thank you Jeff! This is what I am looking for as I was so frustrated yesterday doing macro on the pie chart colors. Although I need more practice since I am just beginning to learn macro. Btw, you are so funny!

  12. Ronald says:

    Thanks for your very handy post.
    Additional, I’ve put a variable in my sub names to avoid them to appear on the macro list.
    This was done also thanks to this forum with:
    “Sub This_is_the_Sub_name(Optional FakeArgument As String)”

    I have a button on the ribbon for every macro (please note that you can also change the button picture) and use it for:

    -one complex containing several macros that I need quite often.
    -Remove all formats [from the previous selected cells]
    -Paste values
    -Paste formats
    -Paste formulas
    -script to replace 99.999,77 notation [remove . & replace ,]
    All above, of-course, applying to previous selected cells.

    Note: I first link the button to the specific macro and add the fake argument after it.

    • Khushnood Viccaji (KV) says:

      That’s interesting Ronald.
      What is the logic of using the fake argument ? This is the first time I’ve read about it :)

      Usually to hide a macro name from the ‘Run Macro’ dialog box, I define that sub as ‘Private’.
      E.g Private Sub DoThis() would ensure that the ‘DoThis’ macro does not show in the dialog box.

      Also note, that defining a sub as Private, may cause other problems, like you cannot call that macro from a subroutine in another module within the same workbook using only the name of the macro.
      In such a case, you would have to give the full qualification of the workbook name to call the macro –
      Application.Run “Book1!DoThis”

  13. Chris says:

    Hi Jeff and Forum Users

    I would like to know what are better a Personal Macro Workbook or an add-in?
    I prefer the add-in because of the menu option that comes with it.
    Any views on this

    Regards

    Chris

  14. […] To see what to do with this code, read What would James Bond have in his Personal Macro Workbook. […]

  15. […] To see what to do with this code, read What would James Bond have in his Personal Macro Workbook. […]

  16. […] folks. Jeff Weir here again. You may remember me from posts such as What would James Bond have in his Personal Macro Workbook and my now infamous music review. Today – and this truly will be music to some ears – […]

Leave a Reply