+ Reply to Thread
Results 1 to 8 of 8

Assign a few items to one key

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

    Assign a few items to one key

    Hi Guys,

    I have a listbox from Table source where User can choose a few rows.

    As a result we can get for example choosen by User:

    Unique Ticket Number Number of Company
    5000020_2016_1 5000020
    5000015_2016_2 5000015
    5000020_2016_3 5000020

    I want to group this numbers of companies to get "something" in code (this is why this topis is created) where i can simply read the specific value.
    I want to have something like this as a result:
    key - 5000020, items assigned to it: 5000020_2016_1, 5000020_2016_3
    key - 5000015, items assigned to it: 5000015_2016_2

    Problem is to find the mutiple values and assigned them to key. If this was the Dictionary that it would be easy for one item assigned to unique key.
    We can assign easy 5000020 for 5000020_2016_1 and 5000015 for 5000015_2016_2.

    But what if we have a few items?

    Thank You in advance for your help,
    Warm Regards,
    Jacek Antek

  2. #2
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Assign a few items to one key

    Hi,

    You may assign an array or another Dictionary to a Dictionary item. In this case one might simply use a delimited string that can be split into an array when required.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

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

    Re: Assign a few items to one key

    Quote Originally Posted by xlnitwit View Post
    Hi,

    You may assign an array or another Dictionary to a Dictionary item. In this case one might simply use a delimited string that can be split into an array when required.
    Thank you for your answer - i will test it surely.

    But i read also that for this purposes Class modules can be used, maybe somebody does it in his/her code ?

    Jacek

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

    Re: Assign a few items to one key

    It wouldn't really make sense to use a class module for that, an array is better suited. You'd use a class where each value has a distinct name (property) and there are known given number of them.

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

    Re: Assign a few items to one key

    It wouldn't really make sense to use a class module for that, an array is better suited. You'd use a class where each value has a distinct name (property) and there are known given number of them.
    Tahnk you Kyle123 for your answer.

    Could you please give me a short example what you mean about:

    You'd use a class where each value has a distinct name (property)
    You are talking about Company numbers ? They should be distinct to use a class?

    Jacek

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

    Re: Assign a few items to one key

    Ok,

    i want to do this using Collection. I have a problem with this.

    My code is:

    Please Login or Register  to view this content.
    Workbook Example in attachment.

    I can add items for collection for number 5000020 but when i have second number 5000015 - i dont know how to add this to the same collection in order to a few items for one number.

    Please help,
    Jacek
    Attached Files Attached Files
    Last edited by jaryszek; 08-31-2016 at 05:32 AM.

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

    Re: Assign a few items to one key

    Ok,

    I have almost done it (I am master !

    My code :

    Sub test()

    Dim i As Integer
    Dim coutner As Integer
    Dim Slowniczek As Dictionary
    Dim sh As Variant
    Dim y As Integer
    Dim test As String
    Dim test2 As String
    Dim Kolekcja As New Collection
    Dim lenght As Long
    Dim VarCompanyNum As String
    Dim del As String
    Dim ArrayRange As Variant

    Set Slowniczek = CreateObject("Scripting.Dictionary")

    With ActiveSheet

    del = "; "

    Dim vitems, vkeys

    For i = 1 To 3
    vitems = Slowniczek.Items
    vkeys = Slowniczek.Keys
    If Not Slowniczek.Exists(ArrayRange(i, 2)) Then
    Slowniczek.Add ArrayRange(i, 2), ArrayRange(i, 1)
    Else
    Slowniczek.Item(ArrayRange(i, 2)) = Slowniczek.Item(ArrayRange(i, 2)) + del + (ArrayRange(i, 1))
    End If
    Next i

    End With

    vitems = Slowniczek.Items
    vkeys = Slowniczek.Keys

    End Sub
    Problem was solved by adding string to string, so in the result there will be:
    key - 5000020, items assigned to it: 5000020_2016_1, 5000020_2016_3
    key - 5000015, items assigned to it: 5000015_2016_2


    only i thing i have to do:

    1. Convert values from multicolumn listbox to 2 dimensional array as string. Any ideas ?

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Assign a few items to one key

    Why do you need/want include the key in the item(s)?

    Isn't the key going to be the same for each item?
    If posting code please use code tags, see here.

+ 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. Assign values to items in a list
    By rk1234 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-21-2014, 09:02 AM
  2. Replies: 3
    Last Post: 01-13-2014, 12:46 PM
  3. [SOLVED] Item Database: Automatically assign number to items(store products)
    By AEvans190 in forum Excel General
    Replies: 18
    Last Post: 10-22-2013, 04:27 PM
  4. Replies: 3
    Last Post: 04-23-2013, 03:46 PM
  5. [SOLVED] Assign a number to items from a keyword in the name
    By samuelabyrd in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-24-2013, 02:07 PM
  6. Identify Items, Sort those Items, Copy the Items
    By a1981stingray in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-14-2010, 10:50 AM
  7. Replies: 6
    Last Post: 09-24-2009, 03:31 PM
  8. Replies: 1
    Last Post: 06-24-2005, 12:21 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