+ Reply to Thread
Results 1 to 5 of 5

Compiling and accessing nested collection within a nested dictionary

  1. #1
    Registered User
    Join Date
    10-29-2012
    Location
    US
    MS-Off Ver
    Excel 365 ProPlus
    Posts
    11

    Compiling and accessing nested collection within a nested dictionary

    *Cleaned up the thread. I was having trouble posting my first thread over the last couple days, so I quickly threw this one together - then it posted (finally).*

    Hi,
    First time posting a thread, long time user/reader of threads. I need help solving a problem that I can't find the answer (or close to the answer) to.

    I have a data-set in a table with 5 columns I care about: Part numbers, Quote date, Quote quantity, price and supplier.
    The first dictionary [DICT1] would host part numbers, with the item being quantity ranges - also I'm presuming as a dictionary [DICT2]. Within the nested dictionary, there will be a collection of the three other variables (date, price, supplier) that is DICT2's item.

    If the supplier is the same, the latest price based on quote date will apply and be updated in the collection accordingly.
    If the supplier is different, the lowest price regardless of quote date will apply and be updated in the collection accordingly.

    Not all DICT1 keys will have all of the keys available in DICT2 (see comparison in the attached sample spreadsheet).
    Pivot tables won't work because it will show all suppliers who have a quantity range price, not just the lowest/latest.

    When the table is evaluated, the output will be the information collected in the dictionary. The format could be something like what is shown in the attached sample file (starting in column K).

    Given (hard-coded in macro for now):
    Quantity Ranges (ie 1 to 5, 6 to 10, etc)
    6 month age restriction for evaluating the data. (older quotes that are in the table will be skipped over).

    I am struggling to access the Coll items in DICT2 for evaluation. Also struggling to dump the information collected into an output. Any help or suggestions would be fantastic!

    Cheers!
    Attached Files Attached Files
    Last edited by jrevball; 03-04-2020 at 10:57 AM.

  2. #2
    Registered User
    Join Date
    10-29-2012
    Location
    US
    MS-Off Ver
    Excel 365 ProPlus
    Posts
    11

    Re: Compiling and accessing nested collection within a nested dictionary

    To offer some more guidance here, I really just need to know the syntax to assess items in the collection that is nested two levels down against a record in the current row I'm in.

    So, for the first collection item (which should always be the same) I do not know the right syntax to access it. I've tried all kinds of ways to access it to no avail. I've tried using both an array(1 to 3) and collection with variables "price", "date", "customer".
    These do not work:
    Dict1(Dict2)(Coll(1))
    Dict1(Dict2).Item(Coll(1))
    Dict1(Dict2).Items(Coll(1))
    Dict1(Dict2(Coll(1)))
    Dict1(Dict2.Item(Coll(1)))
    Dict1(Dict2.Items(Coll(1)))

    ...and the same if you replace Coll(1) with array(1).
    I've also tried the above with Coll.price. It didn't work.

    Stumped! This syntax will help me get the rest of the code in line.

    If [insert syntax here for the first item in array/col] >= "some cell value" then
    do something...
    Else
    replace [insert syntax...] with "some cell value"
    End If

  3. #3
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Compiling and accessing nested collection within a nested dictionary

    I don't know your purpose to mix nesting dictionary and collection, you can solve the problem just using dictionary alone or just collection alone.

    Using dictionary :
    Please Login or Register  to view this content.
    Using collection :
    Please Login or Register  to view this content.
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  4. #4
    Registered User
    Join Date
    10-29-2012
    Location
    US
    MS-Off Ver
    Excel 365 ProPlus
    Posts
    11

    Re: Compiling and accessing nested collection within a nested dictionary

    Karedog- First, thanks! I can't believe how fast the code is. I originally tried using nested dictionaries alone, then quickly got lost trying to wrap my head around the syntax. I appreciate the help!! What you wrote was exactly what I was trying to originally do. In your opinion, is dictionary or collection better in this case?

    Note: I don't think the if..then algorithm to determine if the supplier is the same of the price is higher is quite right. I'll tinker with it a bit to see if I can get results I expect. The basic coding you provided is perfect though!

    Update 2:
    I can't seem to validate the algorithm to a manual check of the real data. I'll keep playing with the if..then statement. In the meantime, here is the logic:

    If a supplier at a given qty range is the same, use the latest date information to decide whether to replace existing data in the collection/dictionary. There can be multiple quotes from the same supplier in the dataset. I want to use the latest date information for that specific qty range in this case.
    If the supplier is different at that qty range (either null or a different supplier), use the lowest price to decide when to replace existing data in the collection/dictionary.

    Scenarios:
    S1 = current supplier at qty range "x", S1 date is newer than current date, replace Price with price at date regardless of which is higher. Otherwise, move to next record.
    S1 <> current supplier at qty range "x", S1 price is lower than the current price, replace current price with S1 price, current supplier with S1 supplier, current date with S1 date, etc.

    If it helps, I can send better "real life" data in a test file, with made up supplier names and part numbers to protect the innocent. Even with a table that has 4900+ lines, your code takes less than a second (impressive)!

    Update 3:
    I figured out the if.then statement yesterday. For posterity:
    If (b(1) = "") Or (b(1) <> a(i, 5) And b(0) > a(i, 4)) Or (b(1) = a(i, 5)) Then
    should be
    If (b(1) = "") Or (b(1) <> a(i, 5) And b(0) > a(i, 4)) Or ((b(1) = a(i, 5)) And ((b(2) >= a(i, 2)))) Then
    'if ((supplier is null OR supplier is different) and $ is greater than current $) OR (supplier is same AND quote date is newer than current date)) then

    Thanks for the help!!
    Last edited by jrevball; 03-06-2020 at 09:47 AM. Reason: adding some feedback

  5. #5
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Compiling and accessing nested collection within a nested dictionary

    You are welcome. Both can be worked nicely, you can choose the one that you like it better.

    Well, I thought that "the latest price based on quote date", the latest here mean the latest row of the same quote date (the way the data is inputted, the latest must be at the very bottom), glad you can found the solution by yourself.

    If you think the case has been solved, please mark this thread as solved :
    select Thread Tools from the menu link above and mark this thread as SOLVED.

    Regards

+ 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. Help to adjust code for nested dictionary to array
    By max138 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-21-2017, 05:40 AM
  2. [SOLVED] Nested vlookup or nested IFAND function?
    By jeptik in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 03-11-2017, 02:27 AM
  3. [SOLVED] Comparing Arrays: What are tradeoffs b/w using a Dictionary vs a nested For-Each loops?
    By joe31623 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-04-2016, 01:29 PM
  4. [SOLVED] Nested Sumif's or how to sum data based on nested criteria
    By dlietz in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 10-07-2014, 11:36 AM
  5. How to reduces Nested IF code(144 Nested IF) to achive same functionality
    By jobseeker in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-09-2014, 09:45 AM
  6. Nested dictionary items, how do you access them?
    By treyr in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-15-2010, 12:23 PM
  7. Collection vs Dictionary
    By MattShoreson in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-12-2007, 10:43 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