PC Review


Reply
Thread Tools Rate Thread

Counting occurrences of textString in variant array

 
 
=?Utf-8?B?RGF2aWQ=?=
Guest
Posts: n/a
 
      29th Mar 2007
Hi,
I'm summarising the contents of a range in a report. Each cell in the range
contains text or is empty. I've loaded the range into a variant array, then
looped thro the array and loaded a NewCollection using the .Add [Key'] arg to
eliminate duplicates. I would now like to loop thro the NewCollection and
count occurrences of each string in the array. What is the neatest way to do
this?
My code:
MyArray = Sheets("MySheet").Range("rng").Value
'// Load Array into collection, eliminate dupes
For r = 1 To UBound(MyArray, 1)
For c = 1 To UBound(MyArray, 2)
If Not IsEmpty(MyArray(r, c)) Then
On Error Resume Next
myCollection.Add MyArray(r, c), "Key " & MyArray(r, c)
On Error GoTo 0
End If
Next
Next

For i = 1 To myCollection.Count
'code required here
Next

Thanks

 
Reply With Quote
 
 
 
 
Bob Phillips
Guest
Posts: n/a
 
      29th Mar 2007
Surely, if you have eliminated duplicates, they all occur just once.

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"David" <(E-Mail Removed)> wrote in message
news:0449084B-AF2B-49ED-AE8D-(E-Mail Removed)...
> Hi,
> I'm summarising the contents of a range in a report. Each cell in the
> range
> contains text or is empty. I've loaded the range into a variant array,
> then
> looped thro the array and loaded a NewCollection using the .Add [Key'] arg
> to
> eliminate duplicates. I would now like to loop thro the NewCollection and
> count occurrences of each string in the array. What is the neatest way to
> do
> this?
> My code:
> MyArray = Sheets("MySheet").Range("rng").Value
> '// Load Array into collection, eliminate dupes
> For r = 1 To UBound(MyArray, 1)
> For c = 1 To UBound(MyArray, 2)
> If Not IsEmpty(MyArray(r, c)) Then
> On Error Resume Next
> myCollection.Add MyArray(r, c), "Key " & MyArray(r, c)
> On Error GoTo 0
> End If
> Next
> Next
>
> For i = 1 To myCollection.Count
> 'code required here
> Next
>
> Thanks
>



 
Reply With Quote
 
JE McGimpsey
Guest
Posts: n/a
 
      29th Mar 2007
One way:

Dim myCollection As Collection
Dim MyArray As Variant
Dim rTemp As Range
Dim r As Long
Dim c As Long
Dim i As Long

Set myCollection = New Collection
Set rTemp = Sheets("MySheet").Range("rng")
MyArray = rTemp.Value
'// Load Array into collection, eliminate dupes
For r = 1 To UBound(MyArray, 1)
For c = 1 To UBound(MyArray, 2)
If Not IsEmpty(MyArray(r, c)) Then
On Error Resume Next
myCollection.Add MyArray(r, c), "Key " & MyArray(r, c)
On Error GoTo 0
End If
Next
Next

For i = 1 To myCollection.Count
Debug.Print myCollection(i), _
Application.CountIf(rTemp, myCollection(i))
Next i

In article <0449084B-AF2B-49ED-AE8D-(E-Mail Removed)>,
David <(E-Mail Removed)> wrote:

> Hi,
> I'm summarising the contents of a range in a report. Each cell in the range
> contains text or is empty. I've loaded the range into a variant array, then
> looped thro the array and loaded a NewCollection using the .Add [Key'] arg to
> eliminate duplicates. I would now like to loop thro the NewCollection and
> count occurrences of each string in the array. What is the neatest way to do
> this?
> My code:
> MyArray = Sheets("MySheet").Range("rng").Value
> '// Load Array into collection, eliminate dupes
> For r = 1 To UBound(MyArray, 1)
> For c = 1 To UBound(MyArray, 2)
> If Not IsEmpty(MyArray(r, c)) Then
> On Error Resume Next
> myCollection.Add MyArray(r, c), "Key " & MyArray(r, c)
> On Error GoTo 0
> End If
> Next
> Next
>
> For i = 1 To myCollection.Count
> 'code required here
> Next
>
> Thanks

 
Reply With Quote
 
JE McGimpsey
Guest
Posts: n/a
 
      29th Mar 2007
The routine eliminates duplicates in the *collection*, not the array.

In article <(E-Mail Removed)>,
"Bob Phillips" <(E-Mail Removed)> wrote:

> Surely, if you have eliminated duplicates, they all occur just once.

 
Reply With Quote
 
Bob Phillips
Guest
Posts: n/a
 
      29th Mar 2007
I quote

I would now like to loop thro the NewCollection and
count occurrences of each string

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"JE McGimpsey" <(E-Mail Removed)> wrote in message
news:jemcgimpsey-(E-Mail Removed)...
> The routine eliminates duplicates in the *collection*, not the array.
>
> In article <(E-Mail Removed)>,
> "Bob Phillips" <(E-Mail Removed)> wrote:
>
>> Surely, if you have eliminated duplicates, they all occur just once.



 
Reply With Quote
 
JE McGimpsey
Guest
Posts: n/a
 
      29th Mar 2007
ah, but to finish the quote...

> I would now like to loop thro the NewCollection and
> count occurrences of each string in the array.


OK, I'm done... :-)

In article <(E-Mail Removed)>,
"Bob Phillips" <(E-Mail Removed)> wrote:

> I quote
>
> I would now like to loop thro the NewCollection and
> count occurrences of each string
>
> "JE McGimpsey" <(E-Mail Removed)> wrote in message
> news:jemcgimpsey-(E-Mail Removed)...
> > The routine eliminates duplicates in the *collection*, not the array.
> >
> > In article <(E-Mail Removed)>,
> > "Bob Phillips" <(E-Mail Removed)> wrote:
> >
> >> Surely, if you have eliminated duplicates, they all occur just once.

 
Reply With Quote
 
NickHK
Guest
Posts: n/a
 
      30th Mar 2007
David,
Why not do the count and remove duplicates at the same time ?
I only public variables rather than Property Let/Get for brevity. You can
easily add any other information to the class if needed.

Private Sub CommandButton1_Click()
Call FixDupes(Sheets("Sheet1").Range("rng"))
End Sub

Public Function FixDupes(argRange As Range) As Long
Dim Cell As Range
Dim MyCollection As Collection
Dim Data As cData
Dim i As Long

Set MyCollection = New Collection

For Each Cell In argRange
If Not IsEmpty(Cell.Value) Then
On Error Resume Next
Set Data = New cData
Data.StrValue = Cell.Value
Data.StrCount = 1

MyCollection.Add Data, Cell.Value

If Err.Number > 0 Then
With MyCollection(Cell.Value)
.StrCount = .StrCount + 1
End With
Err.Clear
End If
End If
Next

For i = 1 To MyCollection.Count
Debug.Print MyCollection(i).StrValue, MyCollection(i).StrCount
Next

End Function

<Class Module; cData>
Public StrValue As String
Public StrCount As Long
</Class Module; cData>

NickHK

"David" <(E-Mail Removed)> wrote in message
news:0449084B-AF2B-49ED-AE8D-(E-Mail Removed)...
> Hi,
> I'm summarising the contents of a range in a report. Each cell in the

range
> contains text or is empty. I've loaded the range into a variant array,

then
> looped thro the array and loaded a NewCollection using the .Add [Key'] arg

to
> eliminate duplicates. I would now like to loop thro the NewCollection and
> count occurrences of each string in the array. What is the neatest way to

do
> this?
> My code:
> MyArray = Sheets("MySheet").Range("rng").Value
> '// Load Array into collection, eliminate dupes
> For r = 1 To UBound(MyArray, 1)
> For c = 1 To UBound(MyArray, 2)
> If Not IsEmpty(MyArray(r, c)) Then
> On Error Resume Next
> myCollection.Add MyArray(r, c), "Key " & MyArray(r, c)
> On Error GoTo 0
> End If
> Next
> Next
>
> For i = 1 To myCollection.Count
> 'code required here
> Next
>
> Thanks
>



 
Reply With Quote
 
=?Utf-8?B?RGF2aWQ=?=
Guest
Posts: n/a
 
      30th Mar 2007
Thanks JE
Nice and straightforward, love it

"JE McGimpsey" wrote:

> One way:
>
> Dim myCollection As Collection
> Dim MyArray As Variant
> Dim rTemp As Range
> Dim r As Long
> Dim c As Long
> Dim i As Long
>
> Set myCollection = New Collection
> Set rTemp = Sheets("MySheet").Range("rng")
> MyArray = rTemp.Value
> '// Load Array into collection, eliminate dupes
> For r = 1 To UBound(MyArray, 1)
> For c = 1 To UBound(MyArray, 2)
> If Not IsEmpty(MyArray(r, c)) Then
> On Error Resume Next
> myCollection.Add MyArray(r, c), "Key " & MyArray(r, c)
> On Error GoTo 0
> End If
> Next
> Next
>
> For i = 1 To myCollection.Count
> Debug.Print myCollection(i), _
> Application.CountIf(rTemp, myCollection(i))
> Next i
>
> In article <0449084B-AF2B-49ED-AE8D-(E-Mail Removed)>,
> David <(E-Mail Removed)> wrote:
>
> > Hi,
> > I'm summarising the contents of a range in a report. Each cell in the range
> > contains text or is empty. I've loaded the range into a variant array, then
> > looped thro the array and loaded a NewCollection using the .Add [Key'] arg to
> > eliminate duplicates. I would now like to loop thro the NewCollection and
> > count occurrences of each string in the array. What is the neatest way to do
> > this?
> > My code:
> > MyArray = Sheets("MySheet").Range("rng").Value
> > '// Load Array into collection, eliminate dupes
> > For r = 1 To UBound(MyArray, 1)
> > For c = 1 To UBound(MyArray, 2)
> > If Not IsEmpty(MyArray(r, c)) Then
> > On Error Resume Next
> > myCollection.Add MyArray(r, c), "Key " & MyArray(r, c)
> > On Error GoTo 0
> > End If
> > Next
> > Next
> >
> > For i = 1 To myCollection.Count
> > 'code required here
> > Next
> >
> > Thanks

>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Array problem: Key words-Variant Array, single-element, type mismatch error davidm Microsoft Excel Programming 7 11th Jun 2011 12:01 AM
Passing Object Array back to Variant array. buzzluck68@hotmail.com Microsoft VB .NET 4 6th Jun 2007 09:28 PM
Array problem: Key words-Variant Array, single-element, type mismatch error davidm Microsoft Excel Programming 1 8th Nov 2005 04:21 AM
ReDim Object array as parameter of Variant array Peter T Microsoft Excel Programming 4 10th May 2005 02:11 PM
variant array containing cel adresses > convert to actual ranges-array Peter Microsoft Excel Programming 5 10th Dec 2003 09:50 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:14 PM.