Subtotal question

apurk45

Board Regular
Joined
Oct 23, 2002
Messages
224
Office Version
  1. 2021
Platform
  1. Windows
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

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
See if you can adapt this to your range...
[Table="width:, class:grid"][tr][td] [/td][td]
B​
[/td][td]
C​
[/td][td]
D​
[/td][td]
E​
[/td][/tr]
[tr][td]
6​
[/td][td][/td][td][/td][td]
£65,600
[/td][td]
12​
[/td][/tr]

[tr][td]
7​
[/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
8​
[/td][td]Range 1[/td][td]Item 1[/td][td]
£6.00​
[/td][td]
500​
[/td][/tr]

[tr][td]
9​
[/td][td]Range 1[/td][td]Item 2[/td][td]
£59.00​
[/td][td]
100​
[/td][/tr]

[tr][td]
10​
[/td][td]Range 1[/td][td]Item 3[/td][td]
£6.00​
[/td][td]
800​
[/td][/tr]

[tr][td]
11​
[/td][td]Range 1[/td][td]Item 4[/td][td]
£5.00​
[/td][td]
100​
[/td][/tr]

[tr][td]
12​
[/td][td]Range 1[/td][td]Item 5[/td][td]
£6.00​
[/td][td]
500​
[/td][/tr]

[tr][td]
13​
[/td][td]Range 1[/td][td]Item 6[/td][td]
£5.00​
[/td][td]
1,000​
[/td][/tr]

[tr][td]
14​
[/td][td]Range 2[/td][td]Item 1[/td][td]
£59.00​
[/td][td]
500​
[/td][/tr]

[tr][td]
15​
[/td][td]Range 2[/td][td]Item 2[/td][td]
£6.00​
[/td][td]
100​
[/td][/tr]

[tr][td]
16​
[/td][td]Range 2[/td][td]Item 3[/td][td]
£6.00​
[/td][td]
800​
[/td][/tr]

[tr][td]
17​
[/td][td]Range 2[/td][td]Item 4[/td][td]
£5.00​
[/td][td]
100​
[/td][/tr]

[tr][td]
18​
[/td][td]Range 2[/td][td]Item 5[/td][td]
£6.00​
[/td][td]
500​
[/td][/tr]

[tr][td]
19​
[/td][td]Range 2[/td][td]Item 6[/td][td]
£5.00​
[/td][td]
1,000​
[/td][/tr]
[/table]

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
Thank you, but I must admit, I'm bit lost with this one.
 
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
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,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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