Quantcast
Channel: White Noise » tools
Viewing all articles
Browse latest Browse all 32

Learning To Use Excel Macros For PPC

$
0
0

If you’re good with Excel’s formulae and pivot tablets you may want to go to the next level and start making your own macros.

A macro is a short program that tells Excel to do a series of actions. You can get a macro to do just about anything you could do manually, which means macros are great tools for automating your work.

Macro (get it?) photography of clockwork by Acid Pix

Why Use Macros?

1. Because you’re doing the same thing over and over again.

2. Because you want to make your own keyboard shortcut.

3. Because you’re doing the same thing over and over and over again.

4. Because you want to do something with a lot of data, and the processor can’t take it.

5. Because you’re doing the same thing over and over and over and over again.

How Do I Use Macros?

The easiest way, hands down, is to record them.

First you need the Developer tab on the ribbon (i.e. the toolbar thing at the top).

On Windows, right click on the ribbon and click ‘Customize the ribbon’. You’ll see a list of Main Tabs – check the box next to ‘Developer’ and click ‘OK’.

The Developer tab in Excel 2013 – other Excels will look similar, but less flat.

The Developer tab in Excel 2013 – other Excels will look similar, but less flat.

Click ‘Record Macro’. Type in a name (but note the name can’t contain spaces). If you want, give it a shortcut key (choosing L, Q or T means you won’t overwrite an existing shortcut). Press ‘OK’. Do whatever it is you want Excel to learn. Click ‘Stop Recording’.

This has the problem that it will precisely copy what you do and can only change the cells affected in a certain way. If you click ‘Use Relative References’ then you can record a macro that affects different cells according to where the cell you initially selected is, but you can’t do anything like copying the selected data and pasting onto the end of a list in another worksheet.

Something More Powerful

The next easiest way to use macros is to record what you can and then look at the code Excel has generated and alter it yourself. This requires a little coding knowledge but, as you’ve got the fall-back of recording actions when you don’t know how to code something, you only need the basics.

If you want to edit a recorded macro, click on the ‘Macros’ button on the Developer tab, select the macro and click ‘Edit’. This will open a ‘Microsoft Visual Basic for Applications’ window with the macro’s code in.

I suggest reading a simple guide to macros such as Excel-Easy’s tutorial, so you know how to do the basics. Then play around, recording your actions in Excel and modifying the code to make it more flexible.

Also, remember that if you have a question, odds are that someone else has asked it before. This probably goes without saying, but searching for what you need is usually the quickest way to find a solution. For example, I wanted to check if a worksheet of a certain name already existed, and a search for “check worksheet exists macro” took to me some pre-coded solutions at the MrExcel message board.

Avoiding Excel Meltdown

You might find (especially with an older computer) that trying to do a bunch of VLOOKUPs or SUMIFs over a lot of data leaves Excel floundering, all processor cores whirring, and your work grinding to a standstill. You can speed things up a bit by splitting up the formulae to automatically look to smaller bits of the spreadsheet.

For example, I was trying to see if an ad group level negative keyword was in all ad groups of a campaign, so the negative could be moved to campaign level. The negative keywords came copied out of AdWords Editor, in columns A to D.

Column A is the campaign, column B is the ad group, column C is the keyword and D the criterion type.

I was using a COUNTIFS, so it looks at everything and then counts what has a matching campaign, a matching keyword and a matching criterion type.

=COUNTIFS($A:$A,$A2,$C:$C,$C2,$D:$D,$D2)

The problem is this doesn’t work very well if there are lots of negative keywords to count through.

So, instead of including the campaign within the COUNTIFS, I used a macro to produce COUNTIFS statements with a different range for each campaign – there were still the same number of COUNTIFS, but instead of counting through over 100 000 negatives each time Excel just had to check one campaign’s worth.

First, we use an AUTOFILTER to order the negatives in (columns A to D) by campaign (column A). To do this, I recorded a macro of me adding a filter and sorting the relevant columns, and then added a comment to explain what the code does:

'Sorts by campaign name
Range("A1:D1").Select
Selection.AutoFilter
ActiveWorkbook.Worksheets("Neg KWs").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Neg KWs").AutoFilter.Sort.SortFields.Add Key:= _
Range("A1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Neg KWs").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Selection.AutoFilter

Then we declare two variables – these will record where the current campaign starts and ends. Row 1 has headers, so row 2 has the first cell of the first campaign – so we initially set the start number to 2.

Dim startRow As Long, endRow As Long
startRow = 2

And we add cell values to give titles to the new columns.

Range("E1").Value = "Repeats in campaign"

Now we get to the bit that will have to repeat for every campaign.

As the campaign names are in alphabetic order we know that if the first campaign name appears (say) 17 times, then all of these 17 appearances will be clumped together, starting with startRow. If the first appearance is in row 2 then the last in row 18. So the endRow should be the number of times the campaign name appears, plus the start row, minus one.

How do we find out how many times the campaign name appears? Application.WorksheetFunction contains all the regular Excel functions, so you can use them within your macro. So we can use Application.WorksheetFunction.CountIf like a normal COUNTIF formula, to count the number of times the campaign name appears.

endRow = Application.WorksheetFunction.CountIf(Range("A:A"), Range("A" & startRow)) + startRow - 1

Now that we know what range we have, we can write a COUNTIFS formula to just cover the current campaign.

'The formula
Range("E" & startRow).Value = "=COUNTIFS($C$" & startRow & ":$C$" & endRow & ",C" & startRow & ",$D$" & startRow & ":$D$" & endRow & ",D" & startRow & ")"

When we have the formula in the first cell of the campaign, we can select the cells

'Copies the formula in the cells for the same campaigns
Range("E" & startRow & ":E" & endRow).Select
Selection.FillDown

(Selection.FillDown is the equivalent of pressing CTRL + D – it duplicates whatever’s in the top cell.)

We then copy the COUNTIFS and paste them as values – this means the values will be saved, and they won’t be recalculated whenever calculations happen.

Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

We then want to do this all again with the next campaign, and the next campaign’s startRow will be just underneath the endRow of the current campaign

startRow = endRow + 1

We then put the code into a While loop, so it will keep going until it runs out of campaigns.

Do While startRow <= Application.WorksheetFunction.CountA(Range("A:A"))
‘insert the code to be repeated here
Loop

Here we’re using a COUNTA function to count the total number of entries in the campaign column, as this is the number of the last row. When the startRow is less than or equal to this number, there is at least one campaign left to go so the loop fires again.

It would be useful to know how many ad groups are in each campaign as well – the easiest way I’ve found to do this is to copy columns A and B (the columns and ad groups) somewhere else in the sheet (L and M, for example) and then remove duplicates. This is something you can record a macro for easily!

'Copy just the campaign and ad group names for counting
Columns("A:B").Select
Range("B1").Activate
Selection.Copy
Columns("L:L").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveSheet.Range("$L$1:$M$91").RemoveDuplicates Columns:=Array(1, 2), Header _
:=xlNo

Then the number of ad groups in a campaign is the number of times that campaign’s name appears in column L where column M isn’t blank. (M is blank where there are campaign level negatives.)

Add into your program before the loop:

Range("F1").Value = "Ad groups in campaign"

And in the loop, next to the other COUNTIFS function, add in:

Range("F" & startRow).Value = "=COUNTIFS($L:$L,A" & startRow & ",$M:$M,""*"")"

(The double double quotes, "", will be treated as part of the text by the macro, and be treated as " when it’s made into a formula in the spreadsheet.)

We then need to adjust the next bit of loop so it copies down the new COUNTIFS in column F as well as the ones in column E.

'Copies the formulae for the cells for the same campaigns
Range("E" & startRow & ":F" & endRow).Select

Then at the end, after the loops, we don’t need the stuff in L or M anymore so we can delete it.

Columns("L:M").Select
Selection.Delete Shift:=xlToLeft

So now the program looks like:

'Sorts by campaign name
Range("A1:D1").Select
Selection.AutoFilter
ActiveWorkbook.Worksheets("Neg KWs").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Neg KWs").AutoFilter.Sort.SortFields.Add Key:= _
Range("A1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Neg KWs").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Selection.AutoFilter

'Copy just the campaign and ad group names for counting
Columns("A:B").Select
Range("B1").Activate
Selection.Copy
Columns("L:L").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveSheet.Range("$L$1:$M$91").RemoveDuplicates Columns:=Array(1, 2), Header _
:=xlNo

'Declare variables and label headers
Dim startRow As Long, endRow As Long
startRow = 2

Range("E1").Value = "Repeats in campaign"
Range("F1").Value = "Ad groups in campaign"

Do While startRow <= Application.WorksheetFunction.CountA(Range("A:A"))

endRow = Application.WorksheetFunction.CountIf(Range("A:A"), Range("A" & startRow)) + startRow - 1

'The formulea
Range("E" & startRow).Value = "=COUNTIFS($C$" & startRow & ":$C$" & endRow & ",C" & startRow & ",$D$" & startRow & ":$D$" & endRow & ",D" & startRow & ")"
Range("F" & startRow).Value = "=COUNTIFS($L:$L,A" & startRow & ",$M:$M,""*"")"

'Copies the formulea for the cells for the same campaigns
Range("E" & startRow & ":F" & endRow).Select
Selection.FillDown
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

startRow = endRow + 1

Loop

Columns("L:M").Select
Selection.Delete Shift:=xlToLeft

The Results

Once it has run through you should see something like this:

The columns after the macro has run.

It turns out in this example that ‘never gonna’ is in all ad groups in the Chorus campaign, so it could be moved to campaign level. ‘You’ is in 5 out of 6, so it might be worth seeing if it should be campaign level as well (although there could be a reason it’s not in one ad group so you would have to use your judgement.)

You download a spreadsheet with this macro in here.

Was this useful? Do you have any tips of your own when starting with macros? Let us know in the comments.

Image credit: macro (get it?) photography of clockwork by Acid Pix

The post Learning To Use Excel Macros For PPC appeared first on White Noise.


Viewing all articles
Browse latest Browse all 32

Latest Images

Trending Articles



Latest Images