Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
BW BW is offline
external usenet poster
 
Posts: 49
Default Retrieve the key (strings) in a Collection

Hi,

If i add items to a Collection using a key String identifers, but then loop
thru items in that Collection using an index, is there any way to retrieve
the corresponding key String associated with that index, or more generally is
it possible to retrieve the key Strings at all?

Thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Retrieve the key (strings) in a Collection

Hi BW,

If i add items to a Collection using a key String identifers, but then
loop
thru items in that Collection using an index, is there any way to retrieve
the corresponding key String associated with that index, or more generally
is
it possible to retrieve the key Strings at all?


As a collection object only has has the a single (Count) property and the
Add, Remove and Items methods, this is not possible.

You could, however, use a scripting dictionary which could return the
information you seek. For example:

'==========================
Public Sub DictionaryDemo()

Dim MyDic ' Create a variable.
Dim MyItems As Variant
Dim myKeys As Variant
Dim Rng As Range
Dim rCell As Range
Dim i As Long

Set Rng = ActiveSheet.Range("A1:A10")

Set MyDic = CreateObject("Scripting.Dictionary")

For Each rCell In Rng.Cells
On Error Resume Next
MyDic.Add rCell.Value, rCell(1, 2).Value
On Error GoTo 0
Next rCell

MyItems = MyDic.items
myKeys = MyDic.keys

For i = 1 To MyDic.Count
Debug.Print MyItems(i - 1), myKeys(i - 1)
Next

End Sub
'<<==========================


---
Regards,
Norman



"BW" wrote in message
...
Hi,

If i add items to a Collection using a key String identifers, but then
loop
thru items in that Collection using an index, is there any way to retrieve
the corresponding key String associated with that index, or more generally
is
it possible to retrieve the key Strings at all?

Thanks



  #3   Report Post  
Posted to microsoft.public.excel.programming
BW BW is offline
external usenet poster
 
Posts: 49
Default Retrieve the key (strings) in a Collection

thanks, I think i will try that.

"Norman Jones" wrote:

Hi BW,

If i add items to a Collection using a key String identifers, but then
loop
thru items in that Collection using an index, is there any way to retrieve
the corresponding key String associated with that index, or more generally
is
it possible to retrieve the key Strings at all?


As a collection object only has has the a single (Count) property and the
Add, Remove and Items methods, this is not possible.

You could, however, use a scripting dictionary which could return the
information you seek. For example:

'==========================
Public Sub DictionaryDemo()

Dim MyDic ' Create a variable.
Dim MyItems As Variant
Dim myKeys As Variant
Dim Rng As Range
Dim rCell As Range
Dim i As Long

Set Rng = ActiveSheet.Range("A1:A10")

Set MyDic = CreateObject("Scripting.Dictionary")

For Each rCell In Rng.Cells
On Error Resume Next
MyDic.Add rCell.Value, rCell(1, 2).Value
On Error GoTo 0
Next rCell

MyItems = MyDic.items
myKeys = MyDic.keys

For i = 1 To MyDic.Count
Debug.Print MyItems(i - 1), myKeys(i - 1)
Next

End Sub
'<<==========================


---
Regards,
Norman



"BW" wrote in message
...
Hi,

If i add items to a Collection using a key String identifers, but then
loop
thru items in that Collection using an index, is there any way to retrieve
the corresponding key String associated with that index, or more generally
is
it possible to retrieve the key Strings at all?

Thanks




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
find and replace numeric strings in larger text strings Mr Molio Excel Worksheet Functions 8 November 9th 11 05:17 PM
How to find number of pairs of strings from list of strings? greg_overholt Excel Worksheet Functions 5 January 27th 06 10:42 PM
Collection does not work with strings? Olaf[_2_] Excel Programming 2 September 27th 04 01:11 PM
Using a collection class to implement mutliple find/replace strings in cells Bill Hertzing Excel Programming 2 February 18th 04 01:42 AM
Finding strings within strings Rod[_6_] Excel Programming 1 December 2nd 03 05:19 PM


All times are GMT +1. The time now is 07:24 PM.

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"