+ Reply to Thread
Results 1 to 11 of 11

Scripting Dictionary add additional items

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-05-2012
    Location
    It's more fun in the Philippines :D
    MS-Off Ver
    Excel 2007
    Posts
    209

    Scripting Dictionary add additional items

    Hi experts,

    I have a code below for scripting dictionary:

                With ThisWorkbook.Sheets("computation")
                    For lctr = 2 To 13
                        aCompensation.Add .Cells(2, lctr), .Cells(5, lctr)
                    Next
                End With
                                
                aType = aCompensation.Keys
                aFruits = aCompensation.Items
    
                'i.e. 
                if aType(1) = "Fruits" then
                      'add items 
                End if
    My question is for example the 1st key is "Fruit" and the 1st item is "Apple",
    is it possible to add "Orange" on the 2nd item of "Fruit" on separate line/loop?

    Thanks in advance!
    thisisgerald
    Last edited by thisisgerald; 11-15-2012 at 06:25 AM. Reason: additional info
    Don't forget to mark your thread as [SOLVED].

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

    Re: Scripting Dictionary add additional items

    1)
    Quote Originally Posted by thisisgerald
                With ThisWorkbook.Sheets("computation")
                    For lctr = 2 To 13
                        aCompensation.Add .Cells(2, lctr), .Cells(5, lctr)
                    Next
                End With
    You can't do it like that.
    Value/Text property MUST be explicitly expressed, otherwise Dictionary object treat them as Range Object.

    2)
                if aType(1) = "Fruits" then
                      'add items 
                End if
    Doesn't make sense.

  3. #3
    Forum Contributor
    Join Date
    09-05-2012
    Location
    It's more fun in the Philippines :D
    MS-Off Ver
    Excel 2007
    Posts
    209

    Re: Scripting Dictionary add additional items

    Quote Originally Posted by jindon View Post
    1)


    You can't do it like that.
    Value/Text property MUST be explicitly expressed, otherwise Dictionary object treat them as Range Object.

    2)
                if aType(1) = "Fruits" then
                      'add items 
                End if
    Doesn't make sense.
    Thanks for the info. I've tried that and it worked (values are on text) but I'll change it as per your advice.
    My problem is I want to add an additional item on the unique key I have made. Basically, there is one item per key and I want it to be two items when certain conditions are met.

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

    Re: Scripting Dictionary add additional items

    It looks like working but not.
    Everything in key/item should be EMPTY.
    Quote Originally Posted by thisisgerald View Post
    My problem is I want to add an additional item on the unique key I have made. Basically, there is one item per key and I want it to be two items when certain conditions are met.
    Few ways.

    1) Utilize Array in item property.
    2) Use special character to join the string in item and split when it needs to be used.
    3) Set another object to item property.

    e.t.c

  5. #5
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Scripting Dictionary add additional items

    Not directly, keys must be unique.

    You could however have a dictionary of dictionaries/arrays so the item with Key "Fruit" would be a dic/array containing "Apple","Orange"

  6. #6
    Forum Contributor
    Join Date
    09-05-2012
    Location
    It's more fun in the Philippines :D
    MS-Off Ver
    Excel 2007
    Posts
    209

    Re: Scripting Dictionary add additional items

    Quote Originally Posted by Kyle123 View Post
    Not directly, keys must be unique.

    You could however have a dictionary of dictionaries/arrays so the item with Key "Fruit" would be a dic/array containing "Apple","Orange"
    Is it possible to add additional item on existing key?

  7. #7
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Scripting Dictionary add additional items

    Only if the item is a datatype capable of holding more than one item

  8. #8
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Scripting Dictionary add additional items

    e.g using array:
    Sub Test()
    Dim arr(0 To 10) As Long
    Dim vKeys: vKeys = Array("Fruits", "vegetables", "cats", "dogs")
    Dim vKey
    Dim dic As Object: Set dic = CreateObject("scripting.dictionary")
    
    For Each vKey In vKeys
        For x = 0 To 10
            arr(x) = x
        Next x
        dic.Add vKey, arr
    Next vKey
    
    Debug.Print "Fruits :" & dic("Fruits")(1)
    Debug.Print "Fruits :" & dic("Fruits")(2)
    Debug.Print "Cats   :" & dic("cats")(3)
    Debug.Print "Cats   :" & dic("cats")(4)
    Debug.Print "Dogs   :" & dic("dogs")(5)
    
    End Sub

  9. #9
    Forum Contributor
    Join Date
    09-05-2012
    Location
    It's more fun in the Philippines :D
    MS-Off Ver
    Excel 2007
    Posts
    209

    Re: Scripting Dictionary add additional items

    Thanks to both of you. I tried other workaround on my conditions.
    I really want to learn that scripting dictionary but I'm running out of time.

    Maybe i'll read other tutorials about that when I'm done on my reports. :D

    Thanks!

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

    Re: Scripting Dictionary add additional items

    See here.

    http://www.excelforum.com/excel-prog...80#post3000680

    The code is little bit too complex, but it uses object (dictionary object) to item property.

  11. #11
    Forum Contributor
    Join Date
    09-05-2012
    Location
    It's more fun in the Philippines :D
    MS-Off Ver
    Excel 2007
    Posts
    209

    Re: Scripting Dictionary add additional items

    @jindon

    Thanks for the link!
    I can start with that one.

    Best regards,
    thisisgerald

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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