Part 2 of 2 - Many Excel macros are available as raw code throughout the internet. Learn where to deploy this code in Excel, how to save it for use in all Excel workbooks, and how to create a convenient shortcut button for using that macro in your everyday workflow.
Fueling A_B experiments with behavioral insights (1).pdf
Saving and Running Excel Macros
1. macros
Saving and Running
Many of the useful Excel macros you’ll find on AcceleratedSEM.com - and
on the internet - are posted as raw text. For this reason, it’s useful to
know how to deploy this code to your instance of Excel.
2. macros
Saving and Running
- Where to paste your macro code in Excel.
- How to save that macro so it can be used in any workbook.
- How to make a customized menu button for your new macro.
3. First: A bit about Excel macro code
Excel macros are written in a programming language called Visual Basic
for Applications (VBA). Here’s an example snippet:
Sub Hyperlinker()
Dim rng As Range, workingRng As Range
Set workingRng = Application.Selection
'Code to set boundaries for workingRng, in case user selects entire row, column, or sheet
Set workingRng = Range(Cells(workingRng.Row, workingRng.Column),
Cells(lastRowInColumn(workingRng.Column), lastColumnInRow(workingRng.Row)))
For Each rng In workingRng
Application.ActiveSheet.Hyperlinks.Add rng, rng.Value
Next rng
End Sub
Private Function lastRowInColumn(columnRef As Long) As Long
lastRowInColumn = Cells(65536, columnRef).End(xlUp).Row
End Function
Private Function lastColumnInRow(rowRef As Long) As Long
lastColumnInRow = Cells(rowRef, Columns.Count).End(xlToLeft).Column
End Function
When this code is executed as an Excel macro, it turns a selection of
plain text URLs into clickable hyperlinks. It’s useful for working with PPC
campaign builds, product feeds, and many analytics reports.
4. Before we get started, a word of warning …
… never run VBA code that you suspect may
be malicious. Doing so can jeopardize your
data and your security.
5. Open a new Excel workbook and click Developer > Visual Basic (all the way left).
The Visual Basic Editor (VBE) will open in a new window.
6. From the VBE’s menu, select Insert > Module
A textbox window will appear inside the Visual Basic Editor.
7. In the new blank textbox window, we’ll paste our macro code.
If you already have a snippet of macro code that you want to implement,
you can use that.
Otherwise, we’ll use the example code from earlier - the macro that
creates hyperlinks from plain text URLs. Click here to retrieve and copy
that code from GitHub.
8. Paste the code into the textbox.
If you used the example code, your window should look like this:
9. Still in the Visual
Basic Editor,
File >> Save
or
CTRL+S
and save as an
Excel Add-In file
(*.xlam).
I’ve saved my
project as
newTool.xlam
10. Close the Visual Basic Editor.
Back in your Excel workbook, click Developer >> Add-Ins
In the resulting dialog box, check the box
that corresponds to the Excel Add-In you
just saved (in my case, newTool.xlam).
If you don’t see your Add-In, you can click
Browse and locate it manually.
11. Sweet! Our new Add-In, and the macro code it
contains, is now ready to be used in every instance of
Excel.
But before we’re done, we want to make a convenient
button for using that macro – the same way we’d use
any of Excel’s built-in tools.
13. Under the Customize Ribbon menu, create a New Tab and New Group.
Select Macros from the dropdown on the left and add your macro to the new group.
(If two Hyperlinker macros are visible, hover over them with your cursor to show their path and choose the one saved as an .xlam)
14. Your macro is now available under it’s own tab in the Ribbon.
You also may have noticed on the preceding screen that you can customize the
name and appearance of your tab. After some finagling, mine looks like this:
Whatever works for you
15. Nice work! As a reward, here’s a picture of a puppy:
Also, you’ve now got a useful tool that will hopefully
make your life a little easier & more productive!
For a demo of the hyperlinker snippet at work, click here.
Next steps: Explore more Excel SEM tools.