Collection' ITEM değerinden KEY değerine ulaşma..

tamer42

Destek Ekibi
Destek Ekibi
Katılım
11 Mart 2005
Mesajlar
3,049
Excel Vers. ve Dili
Office 2013 İngilizce
Merhaba,
Collection nesnesinde ITEM değerinden KEY değerine nasıl ulaşabilirz?

Özetle Aşağıdaki kodda "Google" değerinde "33" değerine nasıl oluşabiliriz?

desteğiniz için şimden teşekkürler,

iyi Geceler

Kod:
Sub test()
Dim iCtr As Integer
Dim itemCount As Integer

Dim myData As Collection
Set myData = New Collection

Dim searchFor As String

myData.Add "MS", "11"
myData.Add "Oracle", "22"
myData.Add "Google", "33"

'** Searching based on value
searchFor = "Google"

itemCount = myData.Count
For iCtr = 1 To itemCount
    If myData(iCtr) = searchFor Then
        MsgBox myData(iCtr)
        Exit For
    End If
Next

'** Searching by key
MsgBox myData.Item("22")
End Sub
 

Korhan Ayhan

Administrator
Yönetici
Admin
Katılım
15 Mart 2005
Mesajlar
42,245
Excel Vers. ve Dili
Microsoft 365 Tr-En 64 Bit
Böyle yapabilirsiniz.

C++:
Option Explicit

Sub Test()
    Dim myData As Dictionary
    
    Set myData = New Dictionary
    
    myData.Add "MS", "11"
    myData.Add "Oracle", "22"
    myData.Add "Google", "33"
    
    MsgBox myData.Item("Google")
End Sub
 

tamer42

Destek Ekibi
Destek Ekibi
Katılım
11 Mart 2005
Mesajlar
3,049
Excel Vers. ve Dili
Office 2013 İngilizce
Böyle yapabilirsiniz.

C++:
Option Explicit

Sub Test()
    Dim myData As Dictionary
   
    Set myData = New Dictionary
   
    myData.Add "MS", "11"
    myData.Add "Oracle", "22"
    myData.Add "Google", "33"
   
    MsgBox myData.Item("Google")
End Sub
Korhan Hocam teşekkürler,
Anladığım kadarıyla Collection yöntemi ile bunu yapmak mümkün değil
 

Korhan Ayhan

Administrator
Yönetici
Admin
Katılım
15 Mart 2005
Mesajlar
42,245
Excel Vers. ve Dili
Microsoft 365 Tr-En 64 Bit
Böyle deneyebilirsiniz.

C++:
Option Explicit

Sub Test()
    Dim myData As Collection
    
    Set myData = New Collection
    
    myData.Add "11", "MS"
    myData.Add "22", "Oracle"
    myData.Add "33", "Google"
    
    MsgBox myData.Item("Google")
End Sub
 

tamer42

Destek Ekibi
Destek Ekibi
Katılım
11 Mart 2005
Mesajlar
3,049
Excel Vers. ve Dili
Office 2013 İngilizce
Böyle deneyebilirsiniz.

C++:
Option Explicit

Sub Test()
    Dim myData As Collection
   
    Set myData = New Collection
   
    myData.Add "11", "MS"
    myData.Add "22", "Oracle"
    myData.Add "33", "Google"
   
    MsgBox myData.Item("Google")
End Sub
Merhaba,
Aşağıdaki kod ile userform üzerindeki CheckBox ' ları bir Collection içine alıyoruz,
buraya kadar bir sıkıntı yok,

Buradan key değerlerini CheckBox.Tag nasıl çakebiliriz?

For Each aKey In Coll
MsgBox Coll.Item(aKey)
Next

gibi...


Kod:
Set Coll = New Collection

For Each ctr In me.Controls
   
   If TypeName(ctr) = "CheckBox" Then
        tg = ctr.Tag
        If Left(tg, 2) = "P0" Then
           Coll.Add ctr.Name, tg
            x = x + 1
        End If
   End If
   
Next
teşekkürler, iyi çalışmalar.
 

tamer42

Destek Ekibi
Destek Ekibi
Katılım
11 Mart 2005
Mesajlar
3,049
Excel Vers. ve Dili
Office 2013 İngilizce
Böyle yapabilirsiniz.

C++:
Option Explicit

Sub Test()
    Dim myData As Dictionary
   
    Set myData = New Dictionary
   
    myData.Add "MS", "11"
    myData.Add "Oracle", "22"
    myData.Add "Google", "33"
   
    MsgBox myData.Item("Google")
End Sub
Korhan Hocam aşağıdaki şekilde bir kod buldum, ihtiyacı olan arkadaşların işine yarayabilir


Class KeyValue:
Kod:
Public key As String
Public value As String
Public Sub Init(k As String, v As String)
    key = k
    value = v
End Sub
- - - - - - - - - - - - - - - - - -
Module:
Kod:
Public Sub Test()
    Dim col As Collection, kv As KeyValue
    Set col = New Collection
    Store col, "first key", "first string"
    Store col, "second key", "second string"
    Store col, "third key", "third string"
    For Each kv In col
        Debug.Print kv.key, kv.value
    Next kv
End Sub

Private Sub Store(col As Collection, k As String, v As String)
    If (Contains(col, k)) Then
        Set kv = col(k)
        kv.value = v
    Else
        Set kv = New KeyValue
        kv.Init k, v
        col.Add kv, k
    End If
End Sub

Private Function Contains(col As Collection, key As String) As Boolean
    On Error GoTo NotFound
    Dim itm As Object
    Set itm = col(key)
    Contains = True
MyExit:
    Exit Function
NotFound:
    Contains = False
    Resume MyExit
End Function
 

tamer42

Destek Ekibi
Destek Ekibi
Katılım
11 Mart 2005
Mesajlar
3,049
Excel Vers. ve Dili
Office 2013 İngilizce
Korhan Hocam aşağıdaki şekilde bir kod buldum, ihtiyacı olan arkadaşların işine yarayabilir


Class KeyValue:
Kod:
Public key As String
Public value As String
Public Sub Init(k As String, v As String)
    key = k
    value = v
End Sub
- - - - - - - - - - - - - - - - - -
Module:
Kod:
Public Sub Test()
    Dim col As Collection, kv As KeyValue
    Set col = New Collection
    Store col, "first key", "first string"
    Store col, "second key", "second string"
    Store col, "third key", "third string"
    For Each kv In col
        Debug.Print kv.key, kv.value
    Next kv
End Sub

Private Sub Store(col As Collection, k As String, v As String)
    If (Contains(col, k)) Then
        Set kv = col(k)
        kv.value = v
    Else
        Set kv = New KeyValue
        kv.Init k, v
        col.Add kv, k
    End If
End Sub

Private Function Contains(col As Collection, key As String) As Boolean
    On Error GoTo NotFound
    Dim itm As Object
    Set itm = col(key)
    Contains = True
MyExit:
    Exit Function
NotFound:
    Contains = False
    Resume MyExit
End Function
Alternatif 2:

Class module: "CollectionKEY"
Kod:
Private mCol As Collection
 
Public Sub Add(Item As Variant, Optional sKey As String, Optional Before As Variant, Optional After As Variant)

    Dim objNewMember() As Variant

    ReDim objNewMember(0 To 1)
    
    If IsObject(Item) Then
        Set objNewMember(0) = Item
    Else
        objNewMember(0) = Item
    End If
    
    objNewMember(1) = sKey
    
    If Len(sKey) = 0 Then
        mCol.Add objNewMember, , Before, After
    Else
        mCol.Add objNewMember, sKey, Before, After
    End If
End Sub
 
Public Property Get Item(vntIndexKey As Variant) As Variant
    Dim vItem() As Variant
    vItem = mCol(vntIndexKey)
    
    If IsObject(vItem(0)) Then
        Set Item = vItem(0)
    Else
        Item = vItem(0)
    End If
End Property
 
Public Property Get ItemKEY(vntIndexKey As Variant) As String
    ItemKEY = mCol(vntIndexKey)(1)
End Property
 
Public Property Get Count() As Long

    Count = mCol.Count
End Property
 
Public Sub Remove(vntIndexKey As Variant)
    mCol.Remove vntIndexKey
End Sub
 
Public Property Get NewEnum() As IUnknown

    Set NewEnum = mCol.[_NewEnum]
End Property
 
Private Sub Class_Initialize()
    Set mCol = New Collection
End Sub
 
Private Sub Class_Terminate()

    Set mCol = Nothing
End Sub

Module:
Kod:
Private Sub Testing()
    Dim ClsKey As New CollectionKEY, Itm As Variant
    Dim K As Long
    
    ClsKey.Add "testing... 1", "KEY1"
    ClsKey.Add "testing... 2", "KEY2"
    ClsKey.Add "testing... 3", "KEY3"
    ClsKey.Add "testing... 4", "KEY4"
    ClsKey.Add "testing... 5", "KEY5"
    
    For K = 1 To ClsKey.Count
        Debug.Print ClsKey.Item(K), ClsKey.ItemKEY(K)
    Next K
    
    
End Sub
 
Üst