15
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

Saving and Running Excel Macros

Embed Size (px)

Citation preview

Page 1: Saving and Running Excel Macros

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

Page 2: Saving and Running Excel Macros

- 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

Page 3: Saving and Running Excel Macros

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.

Page 4: Saving and Running Excel Macros

… 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 …

Page 5: Saving and Running Excel Macros

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.

Page 6: Saving and Running Excel Macros

From the VBE’s menu, select Insert > Module

A textbox window will appear inside the Visual Basic Editor.

Page 7: Saving and Running Excel Macros

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.

Page 8: Saving and Running Excel Macros

Paste the code into the textbox.

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

Page 9: Saving and Running Excel Macros

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

Page 10: Saving and Running Excel Macros

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.

Page 11: Saving and Running Excel Macros

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.

Page 12: Saving and Running Excel Macros

Start by clicking File >> Options

Page 13: Saving and Running Excel Macros

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)

Page 14: Saving and Running Excel Macros

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

Page 15: Saving and Running Excel Macros

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.