Subtotal question

apurk45

Board Regular
Joined
Oct 23, 2002
Messages
221
Hello
Col A (A4:A1500) is populated with part numbers using formula =IF(' Parts List'!A4 <>"",' Parts List'!A4,"").

I'm trying to count number of part numbers in Col A only that are displayed (filtered). I tried this formula, but obviously is wrong.

=SUBTOTAL(3,SUMPRODUCT(--(LEN(A4:A1500)>0)))

I would greatly appreciate if you could help me modify this formula to achieve what I need.

Regards,
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
See if you can adapt this to your range...
B​
C​
D​
E​
6​
£65,600
12​
7​
8​
Range 1Item 1
£6.00​
500​
9​
Range 1Item 2
£59.00​
100​
10​
Range 1Item 3
£6.00​
800​
11​
Range 1Item 4
£5.00​
100​
12​
Range 1Item 5
£6.00​
500​
13​
Range 1Item 6
£5.00​
1,000​
14​
Range 2Item 1
£59.00​
500​
15​
Range 2Item 2
£6.00​
100​
16​
Range 2Item 3
£6.00​
800​
17​
Range 2Item 4
£5.00​
100​
18​
Range 2Item 5
£6.00​
500​
19​
Range 2Item 6
£5.00​
1,000​
D6=SUMPRODUCT(SUBTOTAL(3,OFFSET(B8,ROW(B8:B19)-ROW(B8),)),D8:D19,E8:E19)
E6=SUMPRODUCT(SUBTOTAL(3,OFFSET(B8,ROW(B8:B19)-ROW(B8),)))
(courtesy of benishiryo from EF)
 
Upvote 0
Here is how it works...
(again, courtesy of benishiryo from EF)
SUMPRODUCT(D8:D19,E8:E19)
a simple SUMPRODUCT like the above would simply give you a result of D8*E8 + D9*E9 + ….. And so on
So that gives you 65,600.

hence, we need to create another set of 1s & 0s to make sure filtered rows are not calculated. SUBTOTAL works great with filters. a simple
SUBTOTAL with function number 3 works like this:
SUBTOTAL(3,B8:B19)
it will count how many non-blank cells exist in B8:B19. what i want is a series of numbers that will give me 1 count or 0 count,
depending on whether the particular cell is filtered. that means i actually want
SUBTOTAL(3,B8)
SUBTOTAL(3,B9)
...all the way to SUBTOTAL(3,B19), but inside a single cell.

to implement such a formula, i need the range to be a dynamic one; using the OFFSET formula. there are 5 arguments you can input
inside the OFFSET, but we're just going to use 2 of them.

=OFFSET(reference,rows,cols,[height],[width])
reference
this is the anchor point. so i placed it in B8

rows
this is how many rows you want it away from the anchor point.
to test it out, try inputting:
=OFFSET(B8,0,0)
it is equivalent to putting
=OFFSET(B8,,)
this means anchor in B8, & moving 0 rows & 0 columns away from B8. so this is exactly like putting:
=B8

since we want it to be looking at B8, then B9, then B10, & so on... the "rows" is where we'll put numbers of 0 to 11. the ROW formula
comes in now. the ROW formula gives you the row number, regardless of what column it's in,
=ROW(B8)
so the above will return 8.

to put in a range, it will become an array. to see the results select the formula & press F9
=ROW(B8:B19)
you should see:
{8;9;10;11;12;13;14;15;16;17;18;19}
since we want to start from 0 to 11, you simply have to minus whatever row number you start in.
=ROW(B8:B19)-ROW(B8)
{0;1;2;3;4;5;6;7;8;9;10;11}

SUBTOTAL(3,OFFSET(B8,ROW(B8:B19)-ROW(B8),))
so if you select the SUBTOTAL & OFFSET formula above & press F9, you should be seeing:
{1;1;1;1;1;1;1;1;1;1;1;1}
this is when nothing is filtered. after filtering, you should see a couple of 0s appearing.

what SUMPRODUCT does is to multiply & add these numbers, so this is how it does it:
so you notice that whatever is filtered wont be calculated because anything multiplies by 0 is 0.
 
Upvote 0
???? lost as well. If column A has part numbers and you want to know how many total part numbers (minus the blanks ... but it will count zero)?

you can put this in a loop.

rowCount = Application.WorksheetFunction.CountA(Range(“A:A”))
 
Upvote 0
???? lost as well. If column A has part numbers and you want to know how many total part numbers (minus the blanks ... but it will count zero)?

you can put this in a loop.

rowCount = Application.WorksheetFunction.CountA(Range(“A:A”))

That is VBA, my suggestion is a regular formula. OP did not ask for VBA and not all members are permitted to run code
 
Upvote 0
Let me try simplify my question.

All cells on A4:A1500 have formula =IF(' Parts List'!A4 <>"",' Parts List'!A4,""), to bring part numbers from another Sheet. Not all A4:A1500 cells are populated since the other sheet has some missing numbers.
Currently in A4:A1500 I have only 891 part numbers listed.
Now, if I use filter in Col D, filtered range A4:A1500 displays only 397 parts. And that is what I need this formula for, since this list is changing daily.

If I use formula =SUBTOTAL(3,A4:A1500) I will get the count as 1497 - that is wrong
If I use formula =SUMPRODUCT(--(LEN(A4:A1500)>0)) I will get 891 - that is also wrong

I hoping this is bit clearer.

Unfortunately I CAN NOT use VBA.

Really greatly appreciate your help.
 
Last edited:
Upvote 0
I already gave you a formula suggestion (not a VBA suggestion) in post #2 . Did you try to adapt that yet?
 
Upvote 0
Thank you Ford, Yes I did use your suggestion. Here is my formula that works for me:

=SUMPRODUCT(--(LEN(A4:A1500)>0),SUBTOTAL(3,OFFSET(A4,ROW(A4:A1500)-ROW(A4),)))

Your time helping me is much appreciated.

All the best,
John
 
Upvote 0
Solution

Forum statistics

Threads
1,214,821
Messages
6,121,755
Members
449,049
Latest member
excelknuckles

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