View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Alan Beban[_2_] Alan Beban[_2_] is offline
external usenet poster
 
Posts: 783
Default Count duplicates in an array?

If the functions in the freely downloadable file at
http://home.pacbell.net/beban are available to your workbook (watch for
word wrap)

Sub testIt3a()
Dim arrColor, arrUniq, arrNum, arrOutput
Dim iRows As Long, i As Long
Dim rng As Range
arrColor = Array("Red", "Blue", "Red", "Orange", "Orange", "Red",
"Green", "Blue")
arrUniq = ArrayUniques(arrColor)
iRows = UBound(arrUniq, 1)
ReDim arrNum(1 To iRows, 1 To 1)
For i = 1 To iRows
arrNum(i, 1) = ArrayCountIf(arrColor, arrUniq(i, 1))
Next
ReDim arrOutput(1 To iRows, 1 To 2)
For i = 1 To iRows
arrOutput(i, 1) = arrUniq(i, 1)
arrOutput(i, 2) = arrNum(i, 1)
Next
Set rng = Range("A1").Resize(iRows, 2)
rng.Value = arrOutput
End Sub

Alan Beban

quartz wrote:
I am using Office 2003 on Windows XP.

I need to be able to count the number of each item in a single element
array, then write out the counts.

For example, if the array contains:
Red, Blue, Red, Orange, Orange, Red, Green, Blue

My result would be:
A1: Red B1: 3
A2: Blue B2: 2
A3: Orange B3: 2
A4: Green B4: 1

If anyone has a function to which I could pass my array to output the counts
or could share some code I could adapt it would be most appreciated.

Thanks much in advance.