Using a Filtered Value in a Formula

Written by Allen Wyatt (last updated December 26, 2022)
This tip applies to Excel 2007, 2010, and 2013


8

Chuck has a worksheet with 16 columns and 622 rows. When he filters on one column to display only those rows that contain a particular value, he would like to change the heading (contained in cell A1) so that it contains whatever value he filtered on. Thus, if Chuck filtered so that only cell values containing the word "Central" are displayed, he wants the heading to change to "Accounts Belonging to Central." He wonders how to grab the value by which he's filtering and then use that value in the formula that is used for the heading.

Excel provides two different ways you can filter your data. You can either use regular filtering (what used to be called AutoFiltering) or you can use advanced filtering. If you are using advanced filtering, then accomplishing what you want is somewhat easier because the criteria are stored in cells, in a criteria table. (I won't go into how to set up an advanced filter here; you can search for it on the ExcelTips site—just use the search box at the upper-right of any page.)

Since the criteria are stored in cells, you can use a formula to put together your heading based on the contents of those cells. Where you'll run into issues is if you specify multiple filtering criteria, which can make the heading formula trickier.

If you are using regular filtering—which it seems like Chuck is probably doing—then the filter specification is not stored in a cell; it is maintained internally by Excel. That makes accessing the information more difficult. Fortunately, it can be extracted with a macro, and someone has already done the "heavy lifting" for Excel users before. (No sense in reinventing the wheel, so to speak.) You can find macros that return filtering criteria at these two locations:

http://j-walk.com/ss/excel/usertips/tip044.htm
http://www.ozgrid.com/VBA/autofilter-criteria.htm

The relatively short routines at either site are set up as user-defined functions, allowing you to specify a cell whose criteria you want to return. Thus, if Chuck applied the "Central" filter to the cells in column C (where the actual filter is in cell C3 for all cells below that), then the heading could be constructed in this way:

="Accounts Belonging to " & FilterCriteria(C3)

This formula relies on the function detailed at the j-walk site; if you used the one at the OzGrid site you'd have to change the formula to reflect the function name defined there. You test the two functions at the two sites to discover which one works best for your needs and your data.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12839) applies to Microsoft Excel 2007, 2010, and 2013.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Using the XIRR Function

One of the financial worksheet functions provided in Excel is the XIRR function. This is used to figure out an internal ...

Discover More

Footnotes for Tables

Word includes a powerful feature that allows you to add footnotes and endnotes to your document. What if you want them at ...

Discover More

Automatic Initial Capitals in Tables

Have you ever started typing words in a table, only to find that Word automatically capitalizes the first word in each ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Not All Rows are Filtered

When you are working with large amounts of data in a worksheet, filtering that data can make the process much simpler. ...

Discover More

Removing Duplicates Based on a Partial Match

Some types of data may have certain fields that contain partially identical information. In such cases you may want to ...

Discover More

Removing Filters and Unhiding Rows and Columns on Multiple Worksheets

Need to remove filters and display all rows and columns in all your worksheets? It is not easy to do manually, but with a ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is five more than 3?

2022-08-24 11:07:43

J. Woolley

The problem re. SpillArray mentioned in my previous comment has been resolved. In older versions of Excel that do not support dynamic arrays, you can use ListAutoFilters with the SpillArray function like this:
=SpillArray(ListAutoFilters(...))
SpillArray will determine and populate the spill range for its array expression argument, simulating a dynamic array.
See https://sites.google.com/view/MyExcelToolbox


2022-08-23 16:02:19

J. Woolley

My Excel Toolbox includes the following dynamic array function to list a worksheet's auto filters (Data > Filter):
=ListAutoFilters([Target],[SkipHeader])
It returns three rows (filter range, head cell's value, filter criteria) for Target's worksheet with one column for each filter plus an optional header column. Target can be a cell or range on any worksheet in an open workbook; if omitted, the formula's cell is assumed. If SkipHeader is FALSE (default), a header will be returned in the first column; otherwise, there will be no header column. You can swap rows and columns like this:
=TRANSPOSE(ListAutoFilters(...))
In older versions of Excel that do not support dynamic arrays, you can enter ListAutoFilters as a CSE array formula with predetermined rows and columns; however, it is NOT compatible with My Excel Toolbox's SpillArray function.
ListAutoFilters is a major update relative to the two "macros" mentioned in the Tip.
See https://sites.google.com/view/MyExcelToolbox


2020-09-20 19:05:18

Scott Kahle

The problem with filter is it only returns values and NOT formulas.

I need a way to select the appropriate rows from a data table and return them in a new worksheet for the purpose of creating estimates. The rows contain formulas, but filter on returns values.

Years ago, I did this with Quatro Pro. I can't find a way with Excel.


2018-02-20 11:40:22

Dave Bonin

I've had a similar need to the problem Allen outlined --
How do you detect whether an autofiltered row is visible or not?

Here's what worked for me:

1) Add a helper column to your worksheet. You can title it "Visible"
if you like.

2) Add the following formula to the cells in that new column. Have
the new formula reference any other column in your data cells that
will always have a value. For this example, let's assume column B
always has values. Enter the following in your helper column...
   = SUBTOTAL( 103, B2 )
Then copy that formula all the way down.

This formula counts the visible cells that are non-blank. Since we
are subtotaling only a singe cell, we get a 1 or a 0. The cell contains
one value or no values. Hidden cells -- and therefore hidden rows --
are ignored.

3) After filtering, if the row is visible, its value in the helper column
will be one. If the row is hidden, it will be zero. You can now use the
results in the helper column in other formulas.

Referring to the problem Allen outlined, you can use INDEX() and
MATCH(), or VLOOKUP() functions to solve the problem without
macros.

4) Here's another variant of the function you can use...
= AND( SUBTOTAL( 103, B2 )) Returns TRUE or FALSE


2018-02-20 09:14:28

Lisa

How timely this updated tip is for me, thanks!


2018-02-19 11:08:31

Scott Gates

The j-walk link is no longer valid. I believe this is his new site http://spreadsheetpage.com/index.php/

I can't tell if the ozgrid page is using menu or ribbon Excel, although they are advertising a menu course. I don't know enough about VBA to know if menu/ribbon even makes a difference.


2015-07-28 11:52:36

howard

do you provide a sample excel spreadsheet to go with this tip?


2013-12-16 08:56:41

Chuck

Many thanks for answering my question!!!


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.