All posts by Olly

Filter table, then delete filtered rows

It’s a pretty common requirement to filter out some values in an Excel table, then delete those rows from the table. It should be straightforward to do this with a little VBA, but it seems to catch lots of people out! Here’s one simple method:

You just need to change the sheet codename, the table name, the field index number, and the criteria value.

It would be simple to replace the user selected values with variables or parameters, so the code becomes more reusable:

This is a simple example, applying a very simple filter. But it’s usually the method for deleting the filtered rows which catches people out, and this approach makes it easy.

Excel Book Recommendation

I am often asked to recommend an Excel book. This isn’t always easy, as different people need different things from a book, depending not only on their Excel ability and aspirations, but also their personality and learning preferences.

Excel 2010 BibleOne consistent recommendation I do give, though, is for John Walkenbach’s Excel Bibles:
Excel 2010 Bible – John Walkenbach – Amazon UK


If you only want to get ONE Excel book, this would be it. There’s a bible for each version of Excel, and they’re an excellent mix of how-to guides, background information and reference material.


Index / Match Formula – Basics

INDEX and MATCH are two of the most powerful and flexible functions in Excel – but they are also two of the least understood. I often meet users who are confident with VLOOKUP and HLOOKUP formulae, but don’t seem to understand INDEX and MATCH. So I thought I’d provide a basic guide to using these functions.


This simply returns a value from a list, based on its position in the list.

The INDEX function requires 2 arguments; The first is the list of values, or range containing the list of values. The second argument is the position in that list from which to return a value.

Example Data

Example:  Who is the 4th name in this list? We can use INDEX to tell us:


Which returns “Roger”.



Now, the MATCH function does the exact opposite of INDEX: it returns the position in a list where a specific value occurs.

The MATCH function requires 3 arguments; The first is the value we want to find, the second is the list (or range containing the list) that we want to look in, and the third argument specifies whether we want an exact match, or the next lowest / highest.

Example DataExample: Which position is Helen in, in our example data:


This formula returns the answer 3.

Note the third argument – 0 – which tells Excel we want an EXACT match.


These functions are each useful on their own. But when we combine them, they become even more powerful. Let’s look at the example data again, and ask the question:
Who drives the Hyundai?

Example DataNow, if the columns were reversed, you could simply use a VLOOKUP, to return the Name based on the Car value. So, you could restructure your data. Or you could add a helper column, to repeat the name in column C, and then use a VLOOKUP.

But it’s not always possible or practical to restructure a workbook, and it’s certainly not efficient to duplicate data. What we really want is a “left-looking” VLOOKUP – and this is where INDEX / MATCH can be used so effectively.

We can use MATCH to return the position in the list of Hyundai:
This returns 6.

Now we can use the INDEX function to find the sixth name in the list:
Which tells us it’s Ann.

Now we can simply combine the functions in one formula:
And with one little formula, we get the answer we wanted!

There are more advanced capabilities of both INDEX and MATCH functions, including 2 dimensional arrays, multiple areas, and returning closest values. But this post covers the basic use of the INDEX and MATCH functions in Excel.

Download Excel fileDownload Example File



Write Excel Data to Text File – Early Binding

Following on from my last post Using VBA to Write Excel Data to Text File – I’ve had this reply from Bernie Deitrick, Excel MVP, who has written a modified version of the code for when early binding is preferred:

Code posted with his permission. Thanks, Bernie!

Using VBA to Write Excel Data to Text File

It’s a fairly common requirement to need a bit of VBA code to write Excel data to a text file.  There are several methods you can use to do this, but here’s an example which I like to use, as it offers good control over the range of data, the structure and format of the text output and – unusually – control over the text encoding / character set. It is really simple to specify UTF-8, UTF-16, ASCII, ISO8859, etc.

Change the parameters to suit your needs – you can use any code you like in this section, as long as the four variables rng, stFilename, stSeparator and stEncoding are assigned.


How to Unpivot Excel Data

All too often, data gets stored in crosstab format in Excel. Whilst crosstabs can be great for presenting data, they’re pretty poor for storing it, as it makes it tricky to manipulate the data. Therefore we often have a requirement to ‘unpivot’, or normalise the data. This is a quick tutorial of one method to unpivot Excel data.

Here’s an example of what we’re working with. This is some meaningless, imaginary data, showing some values grouped in a crosstab by colour columns, and by date rows:

Unpivot Excel data - raw crosstab data

Whilst this seems like a reasonable way to store the information, it actually makes it pretty difficult to answer questions about this data – for example, what percentage of September values were Red?

What we really want is for each row to contain only one value, and for colour to be a column field. Then we can easily use a pivot table to interrogate this data.

We’re going to use a Multiple Consolidation Range pivot table to do the hard work for us.

1. Start off by pressing keys ALT > D > P to open the Pivot Table Wizard dialog box:

Unpivot Excel data - Pivot Wizard Step 1

Choose the ‘Multiple consolidation ranges’ option, then click ‘Next’

2a. In step 2a of the wizard, choose the ‘I will create the page fields’ option, and click ‘Next’

Unpivot Excel data - Pivot WIzard Step 2a


2b. Now we need to add our crosstab data range as a data source for this pivot table. Enter / select the appropriate range, then click ‘Add’. Then click ‘Next’.

Unpivot Excel data - Pivot Wizard Step 2b


3. Choose a location for the intermediate pivot table (it’s a good idea to use a new worksheet, as we can simply delete the entire worksheet when we’re finished). Then click ‘Finish’.

Unpivot Excel data - Pivot Wizard Step 3


4. We now have an ‘intermediate’ pivot table, which looks very similar to our raw data, but has some grand totals. Now we want to drill into the source data for this pivot table, by double clicking on the overall Grand Total value – the cell intersection of the Grand Total column, and Grand Total row – circled red:

Unpivot Excel data - Intermediate pivot table


5. By double clicking to drill into the grand total data source, another worksheet is created, containing a table with our unpivotted data:

Unpivot Excel data - Unpivotted data

You can see that we now have colour as a column field, rather than four separate column headings – which allows us to use this data as a field in a pivot table report.

So, returning to our original example question – what percentage of September values were red? – it is now easy to put a quick pivot table together to answer this:

Unpivot Excel data - Pivot table report output

This is one method to unpivot Excel Data, which works well for simple crosstabs. It won’t deal with all cases – for example, where you already have multiple row fields in your data – but for simple cases like this example dataset, it’s a really quick and easy way of making your data a bit more accessible.

Download Excel file Download Example Workbook


Reset Application

If you’re anything like me, you often have several workbook projects open at one time, a mix from barely developed to pre-deployment beta.

And again, if you’re as forgetful as I am, you have run into a situation where you’ve entered debug part way through some faulty code, been distracted by something else and forgotten that the code which was running had disabled screenupdating, or events, or similar. Then switched blithely to another workbook, and been puzzled as to why your events aren’t firing, or why nothing is calculating as it should… Then spent ages pondering over perfectly good code, only to eventually remember that your last routine aborted and left events disabled. Or a cryptic progress message in your status bar. Or no error messages displaying ever again…

Well, maybe it’s only me! But, in case you recognise this, here’s a tip:

A bit of code like this saved to your Personal Workbook, with a nice little icon in the QAT – quick access toolbar – means that when light begins to dawn that you carelessly left the application somewhat undone, you can restore more normal settings with a simple click.