Results 1 to 5 of 5

Thread: how to use collection exist,,find key index vba x64,vb6

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,955

    how to use collection exist,,find key index vba x64,vb6

    how to use collection exist,,find key index vba x64,vb6?
    Code:
    dim keyDc as  New Dictionary 'Collection
    ** add
    DIM Index as long
       If keyDc.Exists(KEY1) Then
                Index  = keyDc(KEY1)
       End If
    
    With this method, he will perform two loops to check the location of the KEY internally. Okay?
    maybe Dictionary can't find medthod:
    GetKeyIndex(key)?
    Last edited by xiaoyao; May 25th, 2024 at 11:04 PM.

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,835

    Re: how to use collection exist,,find key index vba x64,vb6

    Does this help?

    Code:
    Option Explicit
    
    Private Sub AddKeyValue(dic As Dictionary, col As Collection, key As Variant, value As Variant)
       If Not dic.Exists(key) Then
          dic.Add key, value
          col.Add key
       Else
          dic(key) = value
       End If
    End Sub
    
    Private Function GetKeyIndex(col As Collection, key As Variant) As Long
    Dim i As Long
        
       For i = 1 To col.Count
          If col(i) = key Then
             GetKeyIndex = i
             Exit Function
          End If
       Next i
        
       GetKeyIndex = -1 ' Key not found
    End Function
    
    Public Sub Main()
    Dim keyDc As New Dictionary
    Dim keyIndex As Long
    Dim keyOrder As New Collection
    Dim keyToFind As String
       
        
       AddKeyValue keyDc, keyOrder, "Key1", "Value1"
       AddKeyValue keyDc, keyOrder, "Key2", "Value2"
       AddKeyValue keyDc, keyOrder, "Key3", "Value3"
       AddKeyValue keyDc, keyOrder, "Key4", "Value4"
       AddKeyValue keyDc, keyOrder, "Key5", "Value5"
       
       keyToFind = InputBox$("Key to find:", , "Key1")
       
       keyIndex = GetKeyIndex(keyOrder, keyToFind)
       
       If keyIndex > 0 Then
          MsgBox "Index of " & keyToFind & ": " & keyIndex
       Else
          MsgBox keyToFind & " not found."
       End If
    End Sub

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,547

    Re: how to use collection exist,,find key index vba x64,vb6

    hrhrhrhr
    Code:
    Dim d As New Dictionary
    Dim i As Long
    Dim c As String
    Dim k() As Variant
    Dim s As String
    Dim a() As String
    Dim t() As String
    Dim r As Long
        c = "Entry "
        For i = 1 To 9
            d(c & i) = c & i
        Next
        k = d.Keys
        s = Join(k, "|")
        
        a = Split(s, "Entry 2") 'Or whichever key you are looking for
        If UBound(a) > 0 Then  'We found the key
            t = Split(a(0), "|")
            If UBound(t) >= 0 Then r = UBound(t) Else r = 0
            Debug.Print r
        End If
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,955

    Re: how to use collection exist,,find key index vba x64,vb6

    Code:
    Private Sub AddKeyValue(dic As Dictionary, col As Collection, key As Variant, value As Variant)
       If Not dic.Exists(key) Then
          dic.Add key, value
          col.Add key,id
           id=id+1
       Else
          dic(key) = value
       End If
    End Sub

  5. #5
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,311

    Re: how to use collection exist,,find key index vba x64,vb6

    FYI, RC6.cHashD, cCollection, cSortedDictionary all have an IndexByKey-method (in addition to KeyByIndex).

    Olaf

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width