Excel Movies Database Filter Macro

The Oscar nominations will be announced next Tuesday, February 2nd (oh, Groundhog Day, that was a good movie).

In honour of the occasion, I’ve updated my Excel Movie Database sample file.

Excel Movie Database

I included some movies from the Top 250 Movies list at the IMDb website. You can add movies from your DVD collection, or your Netflix list.

Then use the selection boxes to see movies from a specific category, and/or featuring your favourite actor.

In the previous version, you could only choose Category OR Actor, and now you can choose one or both. (Exciting, I know!)

Or, clear both criteria cells, to see all the movies.

Select a movie category
Select a movie category

Download the Sample File

You can download the Excel movies database file from the Contextures website. It’s in Excel 2003 format, and zipped.

The file contains macros, so you’ll have to enable those to make the file work.

The Perfect Computer

I couldn’t find any movies about Excel, to add to the Excel movies database, but computers play a leading role in several movies. You might remember HAL 9000, from “2001: A Space Odyssey”.

HAL: Let me put it this way, Mr. Amor. The 9000 series is the most reliable computer ever made. No 9000 computer has ever made a mistake or distorted information. We are all, by any practical definition of the words, foolproof and incapable of error.

Yes, that was the dream, way back in 1968, when they made “2001: A Space Odyssey”. Over 40 years later, we haven’t even come close to achieving that goal! Well, maybe it’s not the computers’ fault – the users might be part of the problem.

Random Draw Sinner

Congratulations to Alex Kerin, whose name I selected in the random draw for the Excelerator Quiz giveaway. Here’s his name at the top of the list, after I used the RAND function, and sorted the Rand column in ascending order.

Giveaway20100129

Alex’s prize is a 23″ monitor, plus a keyboard and mouse, courtesy of the PowerPivot team. Thanks to everyone who participated, and to the PowerPivot team, and Megan at Ignite Social Media, who organized the giveaway.

__________

Excel AutoFilter Shows Filter Mode

It’s Price Book publishing week for one of my clients, and we’ve been making lists, and checking them twice. Or 3 or 4 times, or more!

When comparing the new prices to the previous prices, an Excel AutoFilter comes in handy. You can select the same product or model in each workbook, and easily compare item details.

Yes, the widget prices went up a bit this year, so that’s why the assembled parts cost a bit more.

There are written steps and a video below, that show how to use the AutoFilter feature, and workarounds for a problem

Record Count in the Status Bar

Sometimes when you select records with an AutoFilter, the record count appears in the Status Bar, at the bottom left. In this example, I was working with a small table, with 50 records, and only one column had a formula.

I selected File Folder in the Product column, and the Status Bar showed that 3 of the 50 records contained that product. So far, so good.

Record Count in the Status Bar
Record Count in the Status Bar

Status Bar Shows Filter Mode

Then I added another record to the table, and selected a different product from the AutoFilter drop down list. This time the Status Bar showed the rather unhelpful message, “Filter Mode”, instead of the record count.

FilterMode02

Excel 2007 seems to handle this better, but in Excel 2003, and earlier versions, you might see “Filter Mode” if there are more than 50 formulas in the list.

When you apply an AutoFilter, the formula recalculate. If there are lots of formulas to calculate, Excel shows a “Calculating %” message in the Status Bar, so you’ll have something to entertain you while you wait.

Unfortunately, the “Calculating %” message interferes with the record count message in the Status Bar. If the record count message is interrupted, it shows the “Filter Mode” message instead.

You can’t change this behaviour, but there are a couple of workarounds that you can use to find the record count.

Use AutoCalc Instead

If the Status Bar shows “Filter Mode”, you can get the record count from the AutoCalc feature instead.

  1. Right-click on the Status Bar
  2. In the pop-up menu, click Count Nums
  3. Click on the column heading for a column that contains numbers (and no blank cells within the list)

You’ll see the count of visible numbers in the AutoCalc area of the Status Bar.

AutoCalc area of Status Bar
AutoCalc area of Status Bar

Use the SUBTOTAL Function

If you’d rather have the record count show up automatically, you can use the SUBTOTAL function. It ignores the filtered rows, and calculates based on the visible rows only.

For example, with numbers in column D, this formula, with 2 as the first argument, will calculate the COUNT of visible numbers:

=SUBTOTAL(2,D:D)

Use the SUBTOTAL Function
Use the SUBTOTAL Function

If you want to count items in a column that contains text, use 3 as the first argument, and subtract 1 from the result, to account for the heading cell.

=SUBTOTAL(3,B:B)-1

Watch the Excel AutoFilter Video

In this very short video you can see my Excel AutoFilter experiment, and watch the Filter Mode message appear in the Status Bar.

There are no ruggedly handsome math teachers in this video, but it’s fun-filled and action-packed!

There are more Excel AutoFilter Tips on my Contextures website.
___________

Remove Duplicates Command in Excel

In Excel 2003 and earlier versions, you can use an Advanced Filter to remove duplicates. In Excel 2007, there’s a new command on the Ribbon to make it easier to remove duplicates from a list.

Be careful with the Excel 2007 Remove Duplicates feature though – it really removes the duplicates. If you use an Advanced Filter instead, you have the option of hiding duplicates, or creating a unique list in a new location.

How It Works

Update: Jason Morin asked a few questions about the Remove Duplicates feature, and how it works, so I’ll answer the questions here. (Thanks Jason!)

1) Does the new Duplicates capability discern between text strings and numerical values that look the same on the screen?

No, it treats the text strings and numbers the same. If the list has a 10 and a ’10, they’ll be treated as duplicates. There isn’t a settings option that I can see, where you can adjust this. In the Advanced Filter feature, those would be seen as 2 unique items.

2) What about non-visible characters in the cell? Does it consider “Pen” and “Pen ” the same? As a user I would view this as a duplicate, but Excel may not.

No, those won’t be treated as duplicates, because the space character in the second entry makes it different. Advanced Filter would do the same.

3) How does it decide WHICH duplicate to remove in the data set?

The first instance of each item is left, and all subsequent entries are deleted.

4) I assume if I’m working with record set (>1 column), I need to concatenate data from columns to create a unique identifier for each record, then run the Duplicates on the new column I created.

You can use the check marks in the Remove Duplicates dialog box, and select all the columns you want to include. Only if all the included columns are duplicated, will an item be removed. Advanced Filter works the same way, but without the check marks.

Remove Duplicates

In this example, the list in cells A1:A10 contains a few duplicates.

Remove Duplicates 01

Follow these steps to remove the duplicates.

  • Select any cell in the list, or select the entire list
  • On the Ribbon’s Data tab, click Remove Duplicates.

Remove Duplicates 02

  • In the Remove Duplicates dialog box, select the column(s) that you want to remove duplicates from
  • Check the box for My Data Has Headers, if applicable, then click OK.
Remove Duplicates dialog box
Remove Duplicates dialog box
  • A confirmation message will appear, showing the number of duplicates removed, and the number of unique items remaining. Click OK to close the message.

RemoveDups04

Watch the Remove Duplicates Video

Here’s a very short video that shows the steps to remove duplicates in Excel 2007.

______________

Advanced Filter Macro Problem in Excel

I love advanced filters, but this week they caused me some serious grief.
Advanced filters in Excel are quick and powerful, and I use lots of them.

They’re a great way to pull a list of unique items from a table, or send specific data from one sheet to another.

Advanced Filters

People sometimes have trouble with an advanced filter, usually because the headings are incorrect, or there are no headings.

The advanced filter criteria can be a bit tricky too, if you go beyond the basics.

I know about those problems, and am careful to avoid them.

A Simple Filter

This week, as part of a larger procedure, I had an advanced filter, similar to the one shown below.

AdvFilterCode01

Filter for Unique Products

The code was designed to pull a list of unique products from column G, and put it in column K.

In this example, all 7 products in column G are unique, so they all should have been filtered to column K.

However, after I ran the filter, there was nothing in column K, except the heading!

AdvFilterCode00

Leftovers Clog the Filter

I checked, and the headings were an exact match, so that wasn’t the problem.

I checked the code carefully, and everything looked okay. Similar code had run hundreds of times, without a hiccup.

To troubleshoot, I tried to run the advanced filter manually, and in the screen shot below, you can see what appeared in the Advanced Filter dialog box when it opened.

Hmmm. There, in the Criteria Range box, was the range from a previous advanced filter.

AdvFilterCode03

Clear Criteria Range Setting

I cleared out the Criteria Range setting, and the filter ran without problems.

As expected, all 7 products showed up in column K.

AdvFilterCode05

Change Advanced Filter Macro Code

To prevent the macro problem from happening again in the future, I revised the macro code.

Even though the advanced filter didn’t use a criteria range, I added one to the code, with the setting as an empty string.

AdvFilterCode04

Good Housekeeping Prevents Clogging

With that change, the code ran perfectly, even if a previous filter had a criteria range.

To prevent your own advanced filter headaches, add that empty criteria range to your code, if you’re not using a criteria range.

It will clear out the setting, in case a previous advanced filter used a criteria range.

I’ll certainly do that from now on!
__________________

Move Excel Data With Advanced Filter No Macro

In Excel, you can automatically copy data from one sheet to another, without using a macro.

For example, there’s a long list of orders on one worksheet, and you’d like to find your largest orders, and copy those to a different worksheet.

Continue reading “Move Excel Data With Advanced Filter No Macro”

Create a Movie Collection Database in Excel

For a simple database, Excel can do a pretty good job of organizing and reporting your data. This example shows a movie collection database in Excel, but you could set up something similar to keep track of books, sales orders, or almost anything else.

Continue reading “Create a Movie Collection Database in Excel”

Click a Map To Filter Excel Data

In the steps below, I’ll show how to set up an Excel workbook so you can click on a map, and see a list of fictional doctors in the state that you selected. There’s also an Excel sample file that you can download, and try this map technique for yourself!

Click state on map to get list of fictional doctors
Click state on map to get list of fictional doctors

Set up the Map

First, I found a clip art map of the continental USA, with a separate shape for each state, and pasted that into a workbook.

Next, I named the shapes, using the two digit state abbreviation.

  • Click on a state shape, e.g. Washington
  • Click in the Name Box, and type the two digit code – WA
Map of USA with state shapes
Map of USA with state shapes
  • Press the Enter key, to complete the naming.

This will be a good test of your geography knowledge and state abbreviation skills.

Set Up the List of Doctors

On a sheet named Doctors, I entered a list of fictional doctor names, state codes and number of patients.

list of fictional doctor names
list of fictional doctor names

I named the range, DoctorList, using the formula:

  • =OFFSET(Doctors!$A$1,0,0, COUNTA(Doctors!$A:$A), COUNTA(Doctors!$1:$1))

as described in my Dynamic Named Ranges tutorial.

Use an Excel Advanced Filter

To get a list of doctor names for a specific state, I’ll use an advanced filter, and a macro to run the advanced filter.

To use an advanced filter, I’ll set up two ranges in the workbook:

  • 1) Filter Criteria Range
  • 2) Filter Extract Range

Those two steps are explained in the sections below.

1) Set Up the Filter Criteria

On the Map sheet, I set up a criteria range for the filter, with the State heading in cell A1, and a State code in cell A2.

To name this range, I selected cells A1:A2 and typed Criteria in the Name box, then pressed the Enter key.

type Criteria in Name box
type Criteria in Name box

2) Set Up the Filter Extract

Next, to create an extract range, I copied the headings from the Doctor list, and pasted them below the map.

With the pasted cells still selected, I named that range StateList.

This is where the list of fictional doctors will appear, after you click a state in the map.

create an extract range
create an extract range

Add the Filter Macro

To “magically” create the list when the map is clicked, I needed an Excel macro.

So, I added a module to the workbook, and entered some VBA code, shown below.

The macro. named GetDoctorList, uses an Advanced Filter, to extract a list of doctors for the selected state.

Sub GetDoctorList()
On Error Resume Next
Dim strState As String
Dim sh As Shape
Dim wsMap As Worksheet
Dim wsDoctor As Worksheet
Dim wsList As Worksheet
strState = Application.Caller
Set wsMap = Worksheets("Map")
Set wsDoctor = Worksheets("Doctors")
wsMap.Range("A2").Value = strState
wsDoctor.Range("DoctorList").AdvancedFilter _
  Action:=xlFilterCopy, _
  CriteriaRange:=wsMap.Range("Criteria"), _
  CopyToRange:=wsMap.Range("StateList"), Unique:=False
wsMap.Activate
wsMap.Range("A1").Activate
End Sub

Assign the Macro to Shapes

The final step is to assign the GetDoctorList macro to each State shape in the USA map.

You could do this manually, if there were just a few shapes:

  • Right-click on each shape
  • Click the Assign Macro command
  • Select the GetDoctorList macro
  • Click OK

Macro to Assign Macros

It might take quite a while to assign the GetDoctorList macro for all 48 states!

So, I wrote another macro, to do the work for me.

You’ll only have to run this macro once, while setting up the workbook.

The macro is only a few lines long, and it assigns the GetDoctorList macro to each shape on the worksheet named Map.

Sub AddMacro()
Dim sh As Shape
For Each sh In Worksheets("Map").Shapes
    sh.OnAction = "GetDoctorList"
Next
End Sub

Run the Filter

Now, to run the filter, you can click on a state in the map.

When the GetDoctorList macro runs, it does the following steps:

  • First, it puts the state code in cell A2
  • Next, the macro runs an advanced filter, which puts the list of doctors in the extract range, starting in cell B18.

The criteria range, and the extracted list are shown in the screen shot below.

The state shape for California (CA) was clicked, and the filtered list shows the fictional doctors from California.

click on a state in the map
click on a state in the map

Get the Sample File

To get the Excel sample file with the USA map and list of fictional doctors, go to the Excel Filter Sample Files page on my Contextures site.

  • On that page, go to the section named: Filter Files FL0011 – FL0020
  • In that section, look for the sample file named FL0012 – Map Based Filter, and click its download link.
  • The zipped Excel contains macros, so be sure to enable those, if you want to test the map filter macros.

More Excel Filter Examples

Excel FILTER function

Advanced Filter Basics

Advanced Filter Criteria

Advanced Filter Macros

Advanced Filter Criteria Slicers

_________________

AutoFilter by Selection in Excel 2007

A couple of weeks ago I described how you could select a cell in a table, and automatically filter the list based on that cell’s value.

The same feature is available in Excel 2007, using a different technique.

Excel Worksheet List

Using the same example as in the previous post, the East region is selected in the table below.

With a couple of clicks, and no programming, you can add an AutoFilter and filter the table to show only the East region orders.

Excel Worksheet List
Excel Worksheet List

Apply the AutoFilter

In previous versions of Excel, you had to add a toolbar button to use the filter by selection feature.

In Excel 2007, the feature is available in a shortcut menu.

The table doesn’t need to have an AutoFilter currently applied.

  1. In a table in Excel, right-click a cell that contains the criterion you’d like to use. For example, to filter for the East region records, right-click an East cell in the Region column.
  2. On the shortcut menu, click Filter, then click Filter by Selected Cell’s Value

FilterSel02

An AutoFilter is added to the table, if there wasn’t already one in place. The table is filtered, and shows only the East region records.

Remove the Filter

To remove the filter, and show all the records again:

  1. In the Region column heading, click the AutoFilter drop down arrow
  2. Click Clear Filter From “Region”.
Clear an Excel AutoFilter
Clear an Excel AutoFilter

The filter is removed from the Region column, but the AutoFilter feature is still turned on.

For more information on Excel AutoFilters, visit the pages on Excel AutoFilter Basics and Excel AutoFilter Tips on my website.

Paste As Values on a Filtered Sheet

In related news, I recently discovered that the mouse shortcut to copy and paste as values doesn’t work anywhere on a filtered sheet, unless all the records are showing.

Here you can see that it’s not available on the shortcut menu.

PasteValuesMouse03

Use Ribbon Command

You can use other methods to copy and paste as values, such as the Ribbon command, but not the shortcut. I wonder why.

PasteValuesMouse04
______________________

AutoFilter By Selection In Excel

In Excel 2003, you can add a couple of buttons to the toolbar to make it easy to filter a table.

For example, in the table below, the East region is selected. With one click of a button, and no programming, you can add an AutoFilter and filter the table to show only the East region orders. Thanks to Roger Govier for sharing this tip.

Continue reading “AutoFilter By Selection In Excel”