SUBTOTAL and SUMPRODUCT

Sanador

Board Regular
Joined
Sep 24, 2004
Messages
108
In an ongoing attempt to derive some statistics from data, I am experimenting with some formulas using both SUMPRODUCT and the SUBTOTAL command. I've stolen the following formula from other posts which enables me to count the number of unique entries from a list of names:

=SUMPRODUCT((D12:D241<>"")/COUNTIF(D12:D241,D12:D241&""))

Now, there are a couple of manipulations I'd like to make. First, how can I successfully add the SUBTOTAL command to this formula so that when the list is filtered, the calculation uses only the data shown?

Next, how do I then take that formula and add other criteria to the equation. For example, with the formula above I have the number unique entries in column D. The next portion of my post would then update that number when the list is filtered. Finally, how could I then derive the number of these entries also containing data in column T? By adding...

,--($T$12:$T$241<>"")

or something similar to the SUMPRODUCT command?

Thanks as always.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Sanador said:
In an ongoing attempt to derive some statistics from data, I am experimenting with some formulas using both SUMPRODUCT and the SUBTOTAL command. I've stolen the following formula from other posts which enables me to count the number of unique entries from a list of names:

=SUMPRODUCT((D12:D241<>"")/COUNTIF(D12:D241,D12:D241&""))

Now, there are a couple of manipulations I'd like to make. First, how can I successfully add the SUBTOTAL command to this formula so that when the list is filtered, the calculation uses only the data shown?

Next, how do I then take that formula and add other criteria to the equation. For example, with the formula above I have the number unique entries in column D. The next portion of my post would then update that number when the list is filtered. Finally, how could I then derive the number of these entries also containing data in column T? By adding...

,--($T$12:$T$241<>"")

or something similar to the SUMPRODUCT command?

Thanks as always.

If you download and install the latest version (3.9) of the morefunc.xll add-in:

=COUNTDIFF(IF(SUBTOTAL(3,OFFSET(D12,ROW(D12:D241)-ROW(D12),,1)),D12:D241,0),FALSE,0)

which must be confirmed with control+shift+enter.

You can easily add another conditional to the formula...

=COUNTDIFF(IF((SUBTOTAL(3,OFFSET(D12,ROW(D12:D241)-ROW(D12),,1)))*(T12:T241<>""),D12:D241,0),FALSE,0)

again followed by control+shift+enter.
 
Upvote 0
Can't seem to install the new morefunc and am getting an "#NAME?" error as a result. I am on a work computer so that may be the problem. Any ideas or other methods?
 
Upvote 0
Sanador said:
Can't seem to install the new morefunc and am getting an "#NAME?" error as a result. I am on a work computer so that may be the problem. Any ideas or other methods?

Try to install it in your home directory in a folder that you could name AddIns.
 
Upvote 0
Got it... thanks. However, the formula is only counting the items that meet the criteria listed in the first used row (12). For example, given the data set:

Column D E

Row 12 Joe Red
Row 13 Bob Blue
Row 14 Joe Green
Row 15 Jon Red
Row 16 Joe Red
Row 17 Bob Green

If filtered on column E for "Red," the formula should equal 2.
If filtered on column E for "Green," the formula should also equal 2.

Currently the "Red" filter results in 2, but the "Green" or any other filter results in 0.

This sounds like an easy fix. I spologize.
 
Upvote 0
Sanador said:
Got it... thanks. However, the formula is only counting the items that meet the criteria listed in the first used row (12). For example, given the data set:

Column D E

Row 12 Joe Red
Row 13 Bob Blue
Row 14 Joe Green
Row 15 Jon Red
Row 16 Joe Red
Row 17 Bob Green

If filtered on column E for "Red," the formula should equal 2.
If filtered on column E for "Green," the formula should also equal 2.

Currently the "Red" filter results in 2, but the "Green" or any other filter results in 0.

This sounds like an easy fix. I spologize.
Book4
CDEF
7
8Filter:Red
9Distinct Count2
10
11NameColor
12JoeRed
13BobBlue
14JoeGreen
15JonRed
16JoeRed
17BobGreen
18
Sheet1


E8:

=IF(SUBTOTAL(3,E12:E241)=COUNTA(E12:E241),"",INDEX(E12:E241,MATCH(1,SUBTOTAL(3,OFFSET(E12:E241,ROW(E12:E241)-ROW(E12),,1)),0)))

which must be confirmed with control+shift+enter.

Added in order to show you the filter applied to the E-range.

E9:

=COUNTDIFF(IF(SUBTOTAL(3,OFFSET(D12,ROW(D12:D241)-ROW(D12),,1)),D12:D241,0),FALSE,0)

which you must confirm with control+shift+enter.

Shows the count of distinct names depending on the filter appled on the E-range.
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,818
Members
449,049
Latest member
cybersurfer5000

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