Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 43
Default Sum a collection

Hi, I need a macro that counts different items from a range, for example,
column A:
apples
organges
apples
grapes
grapes
grapes
apples

So the Macro will storage values as Apples = 3, Grapes = 3, Oranges = 1
kind of like a countif function,

thanks,


--
Thank you...
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Sum a collection

Here's one way:

HTH,
Bernie
MS Excel MVP

'Add a reference to Microsoft Scripting Runtime.
'Add a standard module to the project.
'Insert the following code into the Module.

Sub Main()
Dim Dict As Dictionary
Dim ItemCount As Integer
Dim myC As Range

Set Dict = New Dictionary
With Dict
'set compare mode
.CompareMode = BinaryCompare

'add items from column A, starting in row 1
Dim myR As Range
Set myR = Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)

For Each myC In myR
If Not .Exists(myC.Value) Then
myVal = Application.CountIf(myR, myC.Value)
.Add Key:=myC.Value, Item:=myVal
myTotal = myTotal + myVal
If myTotal = myR.Cells.Count Then GoTo FoundAll
End If
Next myC

FoundAll:
'extract keys into variant array
keyArray = .Keys
MsgBox "There are " & Dict.Count & " items in your list."
For Each element In keyArray
MsgBox element & " - " & .Item(element)
Next

End With
Set Dict = Nothing
End Sub




"EXCELMACROS" wrote in message
...
Hi, I need a macro that counts different items from a range, for example,
column A:
apples
organges
apples
grapes
grapes
grapes
apples

So the Macro will storage values as Apples = 3, Grapes = 3, Oranges = 1
kind of like a countif function,

thanks,


--
Thank you...


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Collection Key gabch[_8_] Excel Programming 4 March 20th 06 04:40 PM
Collection as parameter stefantem[_15_] Excel Programming 2 January 13th 06 01:01 PM
Collection Todd Huttenstine Excel Programming 4 December 17th 04 09:41 PM
Sum textboxes in a collection Mark[_18_] Excel Programming 1 August 28th 03 04:17 AM


All times are GMT +1. The time now is 11:04 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"