Counting Duplicate enties within a Date


Posted by tee on July 09, 2001 6:59 PM

Hi Everyone

I have a column of dates and a column of items.

I want to count the number of items between a certain date but I want to exclude duplicate items from the count.

eg.
Column 1 Date - Column 2 - Items
01/01/01 - V0100
05/01/01 - V0100
05/01/01 - V0100
20/01/01 - V0100
05/02/01 - V0145
24/02/01 - V0178
15/03/01 - V0123
20/03/01 - V0199
20/03/01 - V0199
16/04/01 - V0101

So for the period 01/01 to 31/01/01 - I would have 1 item.
For the period 01/02/01 to 28/02/01 - I would have 2 items.
For the period 01/03/01 to 31/03/01 - I would have 2 items.
For the period 01/04/01 to 30/04/01 - I would have 1 item.

Any help would be greatly appreciated and many thanks in advance.

Tee

Posted by Damon Ostrander on July 09, 2001 10:37 PM

Hi Tee,

Here is a little custom VBA function that you can use from a worksheet to count the number of unique entries in a range of cells. Perhaps you can use this to do what you want.

Happy computing.

Damon

Function CountUnique(ItemList As Range) As Long

' CountUnique counts the number of cells in a range of cells, but only
' counts cells that have duplicates once. Thus it is like the Excel
' COUNT function except that it only counts unique values.

' Written by: Damon Ostrander
' Date: 5/3/01
' Email: VBAexpert@piadamon.com

Dim nItems As Long
Dim i As Long
Dim j As Long

nItems = ItemList.Cells.Count
CountUnique = 0
For i = 1 To nItems
For j = 1 To i - 1
If ItemList.Cells(i) = ItemList.Cells(j) Then GoTo Duplicate
Next j
CountUnique = CountUnique + 1
Duplicate:
Next i

End Function

Posted by Tee on July 10, 2001 11:22 PM

Many thanks for that - it is just want I wanted but how do I get it to count within a certain date. I have tried
=SUM((MONTH(VISDate)=1)*(CountUnique(DB!B2:B41)))
but it is counting in the whole range not just the month of Jan (1). any ideas. the range
DB!B2:B41 is Jan and Feb.

Tee nItems = ItemList.Cells.Count CountUnique = 0 For i = 1 To nItems For j = 1 To i - 1 If ItemList.Cells(i) = ItemList.Cells(j) Then GoTo Duplicate Next j CountUnique = CountUnique + 1

Posted by Damon Ostrander on July 11, 2001 10:17 PM

Hi Tee,

Here's one way to do it. I modified the routine I gave you slightly so it would only count entries that are not in hidden rows. Thus you can now filter on the date column to display only the dates you want, and the function will only count the dates that are displayed. If, for example, you want to count the unique values for January, temporarily format your date column to display only month. Then add a filter to this column and filter for month=1. CountIFunique will display the unique number of values for that month that are unique.

Here's the updated function:

Function CountIFunique(ItemList As Range) As Long

' CountUnique counts the number of cells in a range of cells, but only
' counts cells that have duplicates once. Thus it is like the Excel
' COUNT function except that it only counts unique values.

' Written by: Damon Ostrander
' Date: 5/3/01
' Email: VBAexpert@piadamon.com

Dim nItems As Long
Dim i As Long
Dim j As Long

nItems = ItemList.Cells.Count
CountIFUnique = 0
For i = 1 To nItems
If Not ItemList.Rows(i).Hidden Then
For j = 1 To i - 1
If Not ItemList.Rows(j).Hidden Then
If ItemList.Cells(i) = ItemList.Cells(j) Then GoTo Duplicate
End If
Next j
CountIFUnique = CountIFUnique + 1
End If
Duplicate:
Next i

End Function

nItems = ItemList.Cells.Count CountUnique = 0 For i = 1 To nItems For j = 1 To i - 1 If ItemList.Cells(i) = ItemList.Cells(j) Then GoTo Duplicate Next j CountUnique = CountUnique + 1



Posted by Aladin Akyurek on July 12, 2001 2:31 PM

Counting TYPES within a Date range

Damon & Tee,

If you're not per se after a single formula, a formula-based solution is also possible.

I'll assume your example data

{35430," V0100";35434," V0100";35434," V0100";35449," V0100";35465," V0145";35484," V0178";35503," V0123";35508," V0199";35508," V0199";35535," V0101 "}

to be in A2:B11. (Those numbers are dates as internally represented, produced by the procedure that I used to re-post the data. )

In C2 enter: 01-02-2001 (start of the criterion date-range)
In C3 enter: 28-02-2001 (end of the crireion date-range)
In C4 enter: =COUNTIF(D:D,"*?*")

In D2 enter: =IF(AND(A2>=$C$2,A2<=$C$3),IF(ISNUMBER(MATCH(B2,$B$1:B1,0)),"",B2),"") [ copy down as far as needed ]

In C4 you'll see the desired count.

A suitable VBA code could perhaps pick up the criterion range and the count and put them somewhere to consolidate. Damon, any comments on this possiblity?

Aladin

===============

Here's one way to do it. I modified the routine I gave you slightly so it would only count entries that are not in hidden rows. Thus you can now filter on the date column to display only the dates you want, and the function will only count the dates that are displayed. If, for example, you want to count the unique values for January, temporarily format your date column to display only month. Then add a filter to this column and filter for month=1. CountIFunique will display the unique number of values for that month that are unique. Here's the updated function: Function CountIFunique(ItemList As Range) As Long