Saving and Running Excel Macros

Preview:

Citation preview

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.

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.

macros

Saving and Running

First: A bit about Excel macro codeExcel 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 sheetSet workingRng = Range(Cells(workingRng.Row, workingRng.Column), Cells(lastRowInColumn(workingRng.Column), lastColumnInRow(workingRng.Row))) For Each rng In workingRngApplication.ActiveSheet.Hyperlinks.Add rng, rng.ValueNext rng End Sub Private Function lastRowInColumn(columnRef As Long) As LonglastRowInColumn = Cells(65536, columnRef).End(xlUp).RowEnd Function Private Function lastColumnInRow(rowRef As Long) As LonglastColumnInRow = Cells(rowRef, Columns.Count).End(xlToLeft).ColumnEnd 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.

… never run VBA code that you suspect may be malicious. Doing so can jeopardize your data and your security.

Before we get started, a word of warning …

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.

From the VBE’s menu, select Insert > Module

A textbox window will appear inside the Visual Basic Editor.

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.

Paste the code into the textbox.

If you used the example code, your window should look like this:

Still in the Visual Basic Editor,File >> Save or CTRL+Sand save as an Excel Add-In file (*.xlam).

I’ve saved my project as newTool.xlam

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.

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.

Start by clicking File >> Options

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)

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

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.