MrExcel's Learn Excel #839 - Filter by Selection

If you like this content, please consider visiting the video on YouTube and subscribe to the MrExcel Channel to show your support!
This video has been published on Jan 8, 2009.
Shawn sends in a great tip for today's podcast. Access has a Filter by Selection icon. Using a few lines of VBA code, you can add this functionality to your personal macro workbook. Episode 839 shows you how.

Here is the code:
Sub Filter_by_Active_Cell()
Dim ColNum As Integer
ColNum = ActiveCell.Column - _
(ActiveCell.CurrentRegion.Column - 1)
Selection.AutoFilter Field:=ColNum, Criteria1:=ActiveCell
End Sub

Sub AutoFilterToggle()
Selection.AutoFilter
End Sub

This blog is the video podcast companion to the book, Learn Excel 97-2007 from MrExcel. Download a new two minute video every workday to learn one of the 377 tips from the book!
maxresdefault.jpg


Transcript of the video:
Hey, welcome back to the MrExcel netcast, I'm Bill Jelen.
Great tip today, sent in by Sean.
Sean had sent a little bit of VBA code-- let me set this up though.
You know we can turn on the Auto Filter or, in Excel 2007, the Filter, and quickly filter data sets.
So we turn on the Auto Filter and then we basically have drop-downs here where we can go through and choose one particular customer.
Well, there's a great feature to access called Filter by Selection.
So let me turn this off, Data, Filter, Turn off the Auto Filter.
And Sean sent some code-- let's take a look at the code-- already had to adjust my personal macro work book-- called Filter By Active Cell, and basically, it's two lines of code.
The first line of code says, "Hey, what column number are we in?" So, you know, if I'm in the Customer column-- let's take a look at the worksheet here, the Customer column, well that's the sixth column in the spreadsheet or the fourth column in the data set.
And once Sean knows that, then he turns on the Auto Filter, filters the current column to the value in the active cell.
So this is pretty cool.
Let's just come here and choose State Farm and I'll press Alt+F8 to run that.
It turns on the filter and we get just the State Farm records.
And then [ Inaudible at 00:01:29 ] We're going to go into Alt+F8; and we're going to choose Filter by Active Cell; and then Options.
Now, Sean used Q and W just because they were easy and right up there in the top left-hand corner.
AutoFilterToggle, we'll make that one be W, click OK, click Cancel.
So now Ctrl+W brings everything back; Ctrl+Q gives us just the records in the selection.
What an amazing little feature.
Now, what i would rather do, is have an icon up in the toolbar.
So in Excel 2003 we'll go to Tools, Customize, and on the left-hand drop-down we'll find Macros, add a new Custom Menu Item up there, right click, Assign Macro, and that will be Filtered by Active cell.
Okay, now, Custom Menu Item, that's a little frustrating to me.
It'll be nice if we had a nice little button image there.
So this is an amazing trick: I'm going to switch over to Access, where we do have this icon-- we have this icon-- and I'm going to go into Customize here in Access, and basically right-click on that icon and say Copy Button Image; now I'll switch back to Excel; right-click here and say Paste Button Image; and now we can say, basically, that we want this to be, not Image and Text, but the default style which gives us just a single icon.
So check this out: I can choose any Cell, maybe like D, E, F, hit the icon, and I've instantly filtered the data set to just that item.
What a cool, little macro.
I mean, it's really just a couple lines of code with a dim statement.
So, I want to thank Sean for sending it in, I'll be sending an Excel Master pin his way for this great idea.
I want to thank you for stopping by, we'll see you next time for another netcast from MrExcel.
[ music ]
 

Forum statistics

Threads
1,213,546
Messages
6,114,253
Members
448,556
Latest member
peterhess2002

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top