Count Unique items in one column if values in other column is greater than zero

Sanjiv

New member
Joined
May 24, 2018
Messages
2
Reaction score
0
Points
0
Excel Version(s)
2013
Hi
Can some one help me
Count Unique items in one column if values in other column is greater than zero
 
.
Code:
Option Explicit


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


Sub howmany()
With Sheet1
    If .Range("B2").Value > 1 Then
        .Range("C1").Value = CountUnique(Selection)
    End If
End With
End Sub
 

Attachments

  • Count Unique.xlsm
    17.4 KB · Views: 9
.
Code:
Option Explicit


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


Sub howmany()
With Sheet1
    If .Range("B2").Value > 1 Then
        .Range("C1").Value = CountUnique(Selection)
    End If
End With
End Sub

Please see


Column A Column B
1234 -230
1235 0
1236 120
1236 220

Count Unique in Column A if value in Column B > 0

In the above case the count is 1
 
Excel 2007 32 bit
A
B
C
D
E
F
1
3​
2
1234
230​
3
1235
0​
4
1236
120​
5
1236
220​
6
7
Highlight A2:A5
8
Click button
9
Answer C1
Sheet: Sheet1
 
@sanjiv
Hi and welcome
please do not quote entire posts unnecessarily. They clutter the thread and make it hard to read. Thanks
 
Hi and welcome
Please,do not crosspost your question on multiple forums without including links here to the other threads on other forums.

Cross-posting is when you post the same question in other forums on the web. The last thing you want to do is waste people's time working on an issue you have already resolved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser) to the cross-post.

Read this to understand why we ask you to do this, and then please edit your first post to include links to any and all cross-posts in any other forums (not just this site).

Do not post any further responses in this thread until a link has been provided to these cross posts.
 
Back
Top