Вы находитесь на странице: 1из 8

Cart Login

Quick, clean, and to the point

Training

Videos

Functions

Formulas

Shortcuts

Blog
Search...

CNHBO:SAUKHAHCNYBNSBCUNG
EXCEL
Qua28biging,bnsnmTTTNTTkinthcvExcelchtrong4gi

Count visible rows only with criteria

XEMNGAY

= SUMPRODUCT((range = criteria) * (SUBTOTAL(3,OFFSET(range,rows,0,1))))

To count visible rows only with criteria, you can use a rather complex formula based on SUMPRODUCT, SUBTOTAL, and OFFSET.

The problem
The SUBTOTAL function can easily generate sums and counts for hidden and non-hidden rows. However, it isn't able to handle criteria (i.e. like
COUNTIF or SUMIF).

The solution
The solution is to use SUMPRODUCT to apply both the SUBTOTAL function (via OFFSET) and the criteria. In the example shown, the formula in
C12 is:
= SUMPRODUCT((C5:C8 = C10) * (SUBTOTAL(103,OFFSET(C5,ROW(C5:C8) MIN(ROW(C5:C8)),0))))

How this formula works


The core of this formula is the array calculations inside of SUMPRODUCT. The rst array applies the criteria, and the second array handles the
"visibility problem".
= SUMPRODUCT(criteria * visibility)
The criteria is applied with part of the formula:
= (C5:C8 = C10)
Which generates an array like this:
{FALSE;TRUE;FALSE;TRUE}

Where TRUE means "meets criteria". Note that because we are using multiplication (*) inside the rst (and only) array given to SUMPRODUCT, the
TRUE FALSE values will automatically be converted to:
{0;1;0;1}
The visibility lter is applied using SUBTOTAL.
SUBTOTAL is able to exclude hidden rows in a variety of calculations, so we can use it in this case to generate a " lter" to exclude hidden rows
inside of SUMPRODUCT. The problem though is that SUBTOTAL returns a single number, while we need an array to use it successfully inside
SUMPRODUCT.
The trick is to use OFFSET to feed SUBTOTAL one reference per row, so that OFFSET will return one result per row.
Of course, that requires another trick, which is to give OFFSET an array that contains one number per row, starting with zero. We do that using:
= ROW(C5:C8) MIN(ROW(C5:C8)
Which will generate an array like this:
{0;1;2;3}
So, the second array, which handles visibility using SUBTOTAL, is generatedlike this:
= SUBTOTAL(103,OFFSET(C5,ROW(C5:C8) MIN(ROW(C5:C8)),0))
= SUBTOTAL(103,OFFSET(C5,{0;1;2;3},0))
= SUBTOTAL(103,{"East";"West";"Midwest";"West"})
= {1;0;1;1}
And, nally, we have:
= SUMPRODUCT({0,1,0,1} * {1;0;1;1})
Which returns 1.

Good links
MrExcel discussion, with Mike Girvin and Aladin Akyurek
Mike Girvin's Magic Trick 1010

Author
shares

Dave Bruns

Excel Formula Training


Bite-sized videos in plain English.Learn nested IF, VLOOKUP, INDEX &
MATCH, COUNTIFS, RANK, SUMIFS, SMALL, LARGE, andmany formulas to
handle dates and text. Master absolute and relative addresses, named ranges,
errors, andtroubleshooting. Instant access with full guarantee.Watch sample
videos here.

300 Formula Examples, thoughtfully explained.

Get quick Excel tips, direct to your inbox

Formulas, functions, shortcuts, pivot tables, productivity. No uff.


Enter your email

7Comments
shares

Recommend

Signup

exceljet

Share

Login

SortbyNewest

Jointhediscussion
NickCoronado22daysago

Whatdoesthefirstvalue'103'insubtotalrepresent?

Reply Share
DaveBruns

Mod >NickCoronado 22daysago

103specifies"COUNTA"behaviorinsideSUBTOTAL.YoucanseeafullsetofcodesontheSUBTOTALpage.
1

Reply Share
NickCoronado>DaveBruns22daysago

Thanksfortheanswer,maybeyoucantellwhythisisnotworking,I'musing102tocountthenumberoffilteredentriesthataregreaterthan0.Doesthis
lookright?
=SUMPRODUCT((C5:C>0)*(SUBTOTAL(102,OFFSET(C5,ROW(C5:C)MIN(ROW(C5:C)),0))))

seemore

seemore

Reply Share
Zeshanamonthago

Howcanwecountuniquevalues(textstrings)invisiblecellsonly?

Reply Share
SteveFoote3monthsago

Canthistechniquebeadaptedtoworkacrosscolumnsinsteadofrows?
shares

Reply Share
Mike7monthsago

HiDave,
InotherSUMPRODUCTexamples,youshowtheneedforthedoubleunary()onthe"criteria"portion.AmIcorrectinsayingthatthisisnotneededherebecause
youdescribe:
"Notethatbecauseweareusingmultiplication(*)insidethefirst(andonly)arraygiventoSUMPRODUCT,theTRUEFALSEvalueswillautomaticallybeconvertedto:
{0101}"
IsthistrueofallmathematicalfunctionsinsideSUMPRODUCT,orjustmultiplication?
Thanks,
Mike

Reply Share

shares

Popular Topics
Functions | Formulas
Pivot Tables
Conditional formatting
VLOOKUP | IF function
Keyboard shortcuts
Excel pros | Books

This is the most easy to understand demo of VLOOKUP I've ever come across. Thanks! - Tawhid
shares

Excel video training


Quick, clean, and to the point.
Learn more

2012-2016 Exceljet.

Home

About

Blog

Contact

Help us

Search

TwitterFacebook
Google+
RSS

Вам также может понравиться