Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
find and replace numeric strings in larger text strings | Excel Worksheet Functions | |||
How to find number of pairs of strings from list of strings? | Excel Worksheet Functions | |||
Collection does not work with strings? | Excel Programming | |||
Using a collection class to implement mutliple find/replace strings in cells | Excel Programming | |||
Finding strings within strings | Excel Programming |