Distinct Count if the value of another field is > 0

Edwardryez

New Member
Joined
Apr 28, 2014
Messages
48
Trying to modify the formula below:
{= SUM(IF(C2:C233<>"",1/COUNTIF(C2:C233,C2:C233)))}
Based on the Values in the column:
H2:H233<>""

How can i put this together?
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
No problem and thanks for the feedback.

We have both got something out of this question.
I got a free lesson from an expert on formula performance so I have some homework to do now!

Regards,
 
Upvote 0
For completeness I thought I would add the results from my homework.

My original formula that was based on CountIfs ran quickly enough on my PC (Intel Q6600 CPU) for small numbers of rows. By small numbers I mean up to about 500.
The much better formula based on Frequency was about five to six times quicker and it maintained this ratio as the rows increased.

I thought I would try a macro solution based on the Dictionary object. Surprisingly (to me at least), this proved to be much faster than either. Even with 100,000 rows it takes less than a second.

So I ran some tests and publish the results here:

Results_zpshslgxaph.png


Rows
CountIfFrequencyMacro
10000.163610.033460.005
20000.626860.101970.009
50003.632710.676230.020
1000014.87723.065310.039
2000058.7012710.866840.080
30000138.271925.158830.206
4000042.118060.211
5000081.786170.285
75000222.517520.480
100000438.648510.847

<colgroup><col span="2"><col><col></colgroup><tbody>
</tbody>

Code:
Sub DistinctCountWithRestrictions()

    Dim ws As Worksheet
    Dim arrListC As Variant
    Dim arrListH
    Dim dicList As Dictionary
    Dim lr As Long

    Set ws = ThisWorkbook.Worksheets("Sheet3")
    Set dicList = New Dictionary

    With ws
        lr = .Cells(.Rows.Count, "C").End(xlUp).Row
        arrListC = .Range("C2:C" & lr)
        arrListH = .Range("H2:H" & lr)
        For i = 1 To UBound(arrListC)
            If Not dicList.Exists(arrListC(i, 1)) Then
                If arrListC(i, 1) <> "" And arrListH(i, 1) <> "" Then
                    dicList.Add arrListC(i, 1), 1
                End If
            End If
        Next
        .Cells(1, "C").Value = dicList.Count
      End With

End Sub
 
Upvote 0
In fact, by removing the "exists" check it runs even quicker (0.6 seconds for 100,000 rows and around a second for a million.).

Code:
Sub DistinctCountWithRestrictions()

    Dim ws As Worksheet
    Dim arrListC As Variant
    Dim arrListH
    Dim dicList As Dictionary
    Dim lr As Long

    Set ws = ThisWorkbook.Worksheets("Sheet3")
    Set dicList = New Dictionary

    With ws
        lr = .Cells(.Rows.Count, "C").End(xlUp).Row
        arrListC = .Range("C2:C" & lr)
        arrListH = .Range("H2:H" & lr)
        For i = 1 To UBound(arrListC)
            If arrListC(i, 1) <> "" And arrListH(i, 1) <> "" Then dicList.Item(Key:=arrListC(i, 1)) = 1
        Next
        .Cells(1, "C").Value = dicList.Count
      End With

End Sub
 
Last edited:
Upvote 0
In fact, by removing the "exists" check it runs even quicker (0.6 seconds for 100,000 rows and around a second for a million.).

Code:
Sub DistinctCountWithRestrictions()

    Dim ws As Worksheet
    Dim arrListC As Variant
    Dim arrListH
    Dim dicList As Dictionary
    Dim lr As Long

    Set ws = ThisWorkbook.Worksheets("Sheet3")
    Set dicList = New Dictionary

    With ws
        lr = .Cells(.Rows.Count, "C").End(xlUp).Row
        arrListC = .Range("C2:C" & lr)
        arrListH = .Range("H2:H" & lr)
        For i = 1 To UBound(arrListC)
            If arrListC(i, 1) <> "" And arrListH(i, 1) <> "" Then dicList.Item(Key:=arrListC(i, 1)) = 1
        Next
        .Cells(1, "C").Value = dicList.Count
      End With

End Sub

RickXL, That was going to be my next future question in June :)
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,935
Members
449,094
Latest member
teemeren

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