+ Reply to Thread
Results 1 to 13 of 13

Search for specific Key using only split number from composite key in dictionary

Hybrid View

jaryszek Search for specific Key using... 12-11-2018, 07:59 AM
jindon Re: Search for specific Key... 12-11-2018, 08:03 AM
jaryszek Re: Search for specific Key... 12-11-2018, 08:07 AM
jindon Re: Search for specific Key... 12-11-2018, 08:10 AM
jindon Re: Search for specific Key... 12-11-2018, 08:20 AM
jaryszek Re: Search for specific Key... 12-11-2018, 09:16 AM
jindon Re: Search for specific Key... 12-11-2018, 09:36 AM
romperstomper Re: Search for specific Key... 12-11-2018, 09:38 AM
jaryszek Re: Search for specific Key... 12-12-2018, 04:06 AM
jindon Re: Search for specific Key... 12-12-2018, 05:13 AM
jaryszek Re: Search for specific Key... 12-12-2018, 05:28 AM
jindon Re: Search for specific Key... 12-12-2018, 05:51 AM
jaryszek Re: Search for specific Key... 12-12-2018, 05:55 AM
  1. #1
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,946

    Search for specific Key using only split number from composite key in dictionary

    Hi,

    Sub test()
    
    Dim dic As Object
    
    Set dic = CreateObject("scripting.dictionary")
    
    KeyValue = Join(Array("EntityName", 1), Chr(2))
    KeyValue2 = Join(Array("EntityName2", 2), Chr(2))
    KeyValue3 = Join(Array("EntityName3", 3), Chr(2))
    
    dic.Add KeyValue, "ok"
    dic.Add KeyValue2, "ok2"
    dic.Add KeyValue3, "ok3"
    
    End Sub
    in this code i am adding pairs text and number as keys.
    And i want to check what key is for number 3.

    So for number 3 i will have "EntityName3" and item "ok3".
    How to lookup in all keys and find the one what i need?

    For exists property of dictionary i would use whole Key but what if i have only Number?
    Like if dic.exists(KeyValue)...

    Best,
    Jacek
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Search for specific Key using only split number from composite key in dictionary

    Is this what you want?
    msgbox dic.keys()(2)
    msgbox dic.items()(2)

  3. #3
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,946

    Re: Search for specific Key using only split number from composite key in dictionary

    thank you Jindon.

    not exactly.

    Assume that you have a cell A1 when you will input "1".

    And you want to find this "1" value in dictionary and know that this is key "KeyValue".

    What you have provided it would be good idea if i will have numbers in order.
    What i will add first "KeyValue3" to dictionary?

    So i want to somehow search within keys of dictionary using number.

    Best,
    Jacek

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Search for specific Key using only split number from composite key in dictionary

    Then you will need to loop through the items to find the value and the value possibly have multiple keys...

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Search for specific Key using only split number from composite key in dictionary

    Ahhh

    like this?
    For i = 0 To dic.Count - 1
        If dic.keys()(i) Like "*" & Chr(2) & "3" Then
            MsgBox dic.keys()(i) & " : " & dic.items()(i)
        End If
    Next
    
    'Or
    For Each e In dic
        If e like "*" & Chr(2) & "3" Then
            MsgBox e & " : " & dic(e)
        End IF
    Next
    Last edited by jindon; 12-11-2018 at 08:26 AM.

  6. #6
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,946

    Re: Search for specific Key using only split number from composite key in dictionary

    ok.

    thank you,

    the code provided it is ok but i want to avoid loops.

    So how to solve the issue if you have 3 dictionaries:

    I have DicParent and 2 characteristics for it: ParentName, ParentID
    Content: "Jacek", 3

    And i have DicChild with 3 characteristics also: ChildName, ChildID, ChildEyesColor
    Content: "Paul", 5, "Black"
    Content: "Michael", 10, "Blue"

    And i have DicToy with 3 characteristics like: ToyName, ToyShape, ToyID
    Content: "Superman", "Figurine", 7
    Content: "Batman", "Car", 6

    and now i want to do something like this:
    ToyCompositeItem1= Join(Array("Figurine", 7), Chr(2))
    ToyCompositeItem2= Join(Array("Car", 6), Chr(2))
    DicToy.add "Superman", ToyCompositeItem1
    DicToy.add "Batman", ToyCompositeItem2
    
    ChildCompositeKey1 =  Join(Array("Paul", 5, "Black"), Chr(2))
    ChildCompositeKey2 =  Join(Array("Michael", 10, "Blue"), Chr(2))
    
    DicChild.add ChildCompositeKey1, DicToy("Superman")
    DicChild.add ChildCompositeKey2, DicToy("Batman")
    
    ParentCompositeKey = Join(Array("Jacek", 3, "Male"), Chr(2))
    
    DicParent.add ParentCompositeKey, DicChild(ChildCompositeKey1)
    I am wondering how to not loop and use exists property in this case (to avoid duplicates within child and toy dictionaries?)
    and how to add 2 childs from DicChild to Parent Jacek?

    Best,
    Jacek
    Last edited by jaryszek; 12-11-2018 at 09:19 AM.

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Search for specific Key using only split number from composite key in dictionary

    This is something confusing you, but I normally do like this.
    Sub test()
        Dim e, s, dic As Object
        Set dic = CreateObject("Scripting.Dictionary")
        dic("Jack") = Array(CreateObject("Scripting.Dictionary"), 3)
        For Each e In Array(Array("Paul", 5, "Black"), Array("Michael", 10, "Blue"))
            dic("Jack")(0)(e(0)) = Array(CreateObject("Scripting.Dictionary"), e(1), e(2))
            For Each s In Array(Array("Superman", "Figurine", 7), Array("Batman", "Car", 6))
                dic("Jack")(0)(e(0))(0)(s(0)) = Array(e(1), e(2))
            Next
        Next
        MsgBox "Parent: Jack" & vbLf & "Child: Paul" & vbLf & "Toy : Superman" & vbLf & _
                Join(dic("Jack")(0)("Paul")(0)("Superman"), ",")
    End Sub

  8. #8
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,018

    Re: Search for specific Key using only split number from composite key in dictionary

    This seems like a terrible setup, but you could always dump the keys, filter them and see what is left:

    theKeys = Filter(dic.keys, Chr(2) & 1)
    If UBound(theKeys) > -1 Then MsgBox theKeys(0)
    for example.
    Everyone who confuses correlation and causation ends up dead.

  9. #9
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,946

    Re: Search for specific Key using only split number from composite key in dictionary

    wow thank you very much my friends!

    rorya, your code will be useful to check what i have.

    jindon, your code is not from this earth like usually but very helpful.
    It is possible to check if for example Child Michael appeared already and was assigned to dic before?

    With exists function?

    Best,
    Jacek

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Search for specific Key using only split number from composite key in dictionary

    Something like this.
    Sub test()
        Dim e, s, dic As Object
        Set dic = CreateObject("Scripting.Dictionary")
        dic("Jack") = Array(CreateObject("Scripting.Dictionary"), 3)
        For Each e In Array(Array("Paul", 5, "Black"), Array("Michael", 10, "Blue"))
            dic("Jack")(0)(e(0)) = Array(CreateObject("Scripting.Dictionary"), e(1), e(2))
            For Each s In Array(Array("Superman", "Figurine", 7), Array("Batman", "Car", 6))
                dic("Jack")(0)(e(0))(0)(s(0)) = Array(e(1), e(2))
            Next
        Next
        MsgBox dic("Jack")(0).exists("Michael"), , "Check if ""Jack"" has ""Michael"""
        MsgBox dic("Jack")(0)("Michael")(0).exists("Astro Boy"), , "Check if ""Michael"" has ""Astro Boy"""
    End Sub

  11. #11
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,946

    Re: Search for specific Key using only split number from composite key in dictionary

    THANK YOU Master,

    with this code i can build hierarchy in this topic

    https://www.excelforum.com/excel-pro...a-objects.html


    what i have to undestand is assigning here:


    dic("Jack")(0)(e(0))(0)(s(0))
    this is adding to key "Jack" to childDictionary key = e(0) the item (0) value from s(0)?

    Best,
    Jacek

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Search for specific Key using only split number from composite key in dictionary

    I have set the Dictionary in first element in the array, so
    dic("Jack")(0) = dictionary for child
    dic("Jack")(0)("Michael")(0) = dictionary for toy for "Michael" of "Jack"

    If you want ParentId then dic("Jack")(1), "Age" of "Michael" of "Jack" = dic("Jack")(0)("Michael")(1) and 2 for color etc...

    Helps?

  13. #13
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,946

    Re: Search for specific Key using only split number from composite key in dictionary

    thank you !

    Helped a lot!

    Jacek

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Search number between specific text
    By T86157 in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 03-02-2018, 06:21 PM
  2. VBA to search for number and return in specific area of spreadsheet
    By DesertRose in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-19-2017, 04:48 PM
  3. [SOLVED] Split data into specific number of rows to new sheets
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-14-2015, 07:36 PM
  4. Search string for words in dictionary
    By sdl2 in forum Excel General
    Replies: 4
    Last Post: 03-15-2015, 05:40 PM
  5. [SOLVED] Split cell after a specific number of character
    By CobraLAD in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-14-2013, 02:33 AM
  6. Search for TEXT and assign a specific number
    By Sub7 in forum Excel General
    Replies: 12
    Last Post: 08-28-2010, 03:05 PM
  7. [SOLVED] Text to Columns - Only want to split SPECIFIC NUMBER of times
    By NewKid in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 02-03-2006, 10:45 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1