Function Count Unique Visible Cells

willastrowalker

New Member
Joined
Aug 28, 2015
Messages
9
Hello,

I am trying to count unique cells in a filtered list. The macro I have to count total unique cells seems to work perfectly. but i'm trying to change it in two ways.. First is to have it count the unique cells that are not filtered/visible (visibility is set my filters, not group & hide).

the next way i'm trying to do it it to make it a countuniqueif function.. eg, =Countuniqueif(a1:a100,b1:b100,"Countme") - column A has potentially unique values, column B has the "Countme" tag.

Any ideas on either would be a huge help!

thanks


Code:
Function CountUnique(ListRange As Range) As Integer


    Dim CellValue As Variant
    Dim UniqueValues As New Collection


    Application.Volatile


    On Error Resume Next


    For Each CellValue In ListRange
              
        UniqueValues.Add CellValue, CStr(CellValue) ' add the unique item
            
    Next


    CountUnique = UniqueValues.Count


End Function
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Untested:

Code:
Function CountUnique(rList As Range, rTag As Range, sTag As String) As Long
  Dim col           As Collection
  Dim cell          As Variant
  Dim i             As Long

  Set col = New Collection

  On Error Resume Next
  For i = 1 To rList.Rows.Count
    If rTag(i, 1).Value = sTag Then
      If Not Rows(rList(i, 1)).Row.Hidden Then
        col.Add Item:=0, Key:=rList(i, 1).Text
      End If
    End If
  Next i

  CountUnique = col.Count
End Function
 
Upvote 0
That doesn't work..

Was thinking something like this (but it still doesn't work.. I think I need to redefine the Listrange range to only show the visible cells?


Code:
Function CountUnique(ListRange As Range) As Integer


    Dim CellValue As Variant
    Dim UniqueValues As New Collection


    Application.Volatile
    Set ListRange = ListRange.SpecialCells(xlCellTypeVisible)


    On Error Resume Next
          
    For Each CellValue In ListRange
              
        UniqueValues.Add CellValue, CStr(CellValue) ' add the unique item
            
    Next


    CountUnique = UniqueValues.Count


End Function
 
Upvote 0
Typo; the line should be
Code:
If Not Rows(rList(i, 1).Row).Hidden Then
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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