+ Reply to Thread
Results 1 to 19 of 19

Using scripting dictionary to do a sumifs

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Using scripting dictionary to do a sumifs

    hi,

    i am enclosing a test macro. In this macro i create a dictionary object to count the number of processA and processB values for a given ID.

    For some reason it is not working

    any ideas?
    Attached Files Attached Files

  2. #2
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Using scripting dictionary to do a sumifs

    What do you think dic_InFl_SiteIds should be?
    Remember what the dormouse said
    Feed your head

  3. #3
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Using scripting dictionary to do a sumifs

    sorry ..........it should be "dic_Ids"

  4. #4
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Using scripting dictionary to do a sumifs

    I figured. Would you also care to be a little more specific than "not working"?

  5. #5
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Using scripting dictionary to do a sumifs

    the first time through when i find an ID that is not in the dictionary i add it to the dictionary like this
    Please Login or Register  to view this content.
    First line adds it to dictionary and 2nd/3rd line re-assigns values in procA and procB to dictionary.......but for some reason its not updating. it stays at zero even if "ProcA_array(LC1, 1)=1"

  6. #6
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Using scripting dictionary to do a sumifs

    Why don't you just use a pivot table?

  7. #7
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Using scripting dictionary to do a sumifs

    The reason for the code not working is that dic_Ids.Item(ID_array(LC1, 1)) returns a copy of the array and that's what you change. You need to use something like this:
    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Using scripting dictionary to do a sumifs

    thanks.

    Based on your code i also came up with a soln.

    Basically you CAN NOT do this assignment and i dont know why:

    Please Login or Register  to view this content.
    but you CAN do this type of assignment:

    Please Login or Register  to view this content.
    i am also enclosing the code i came up with .........thanks again for your help.
    Attached Files Attached Files

  9. #9
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Using scripting dictionary to do a sumifs

    For the reason I gave you. This:
    Please Login or Register  to view this content.
    returns a copy of the array in the Dictionary, so when you do this:
    Please Login or Register  to view this content.
    you only update the copy.

  10. #10
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Using scripting dictionary to do a sumifs

    but why does this work then:
    Please Login or Register  to view this content.
    wouldnt your comments about the "copy" still apply? thanks again for your help

  11. #11
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Using scripting dictionary to do a sumifs

    No because you are assigning the array directly to the dictionary item. In the former case, you have to return the (copy of the) array before you can access an element of it (to read or write).

  12. #12
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Using scripting dictionary to do a sumifs

    Another option see code comments:-
    Results start column "G"
    Please Login or Register  to view this content.
    Regards Mick

  13. #13
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Using scripting dictionary to do a sumifs

    Mick,

    Other than using Q rather than vTemp, is that different from mine? (BTW, you forgot to alter the bit in the Else part)

  14. #14
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Using scripting dictionary to do a sumifs

    To be Honest I've only just seen your code. If I'd seen it before I posted , I would not have bothered !!!!!

  15. #15
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Using scripting dictionary to do a sumifs

    No worries - thought I was missing something.

  16. #16
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Using scripting dictionary to do a sumifs

    i hope you dont think i am totally stupid but i just dont get this statement

    "In the former case, you have to return the (copy of the) array before you can access an element of it (to read or write)"

    i just dont see why i cant make this type of assignment

    Please Login or Register  to view this content.
    i can display to the screen the contents of this
    Please Login or Register  to view this content.
    so why cant i make an assignment to it.........just dont get the "copy thing"

    granted we have a fix i am just trying to understand why could not do what i thought should work

  17. #17
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Using scripting dictionary to do a sumifs

    Whenever you put an array into a Dictionary, or get one out, you are dealing with a copy. So if you create an array variable, load it into a Dictionary and then alter the original array variable, the Dictionary contents don't change.

    In order to access any element of the array, you have to return the whole array first. In the case of your code, there is an implicit variable created similar to my vTemp variable, which is a copy of what is in the Dictionary (that's just the way it works).

    Reading the copy (e.g. outputting to messages/immediate window) is fine because it's a copy - so the contents are the same as the array in the Dictionary.

    Writing to an element also changes the copy only.

    The only way to update the array contents is to assign a new array back to the Dictionary item.

    I don't really know how else to put it I'm afraid.

    BTW, the same is true of the Collection object.
    Last edited by romperstomper; 10-23-2014 at 11:01 AM.

  18. #18
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Using scripting dictionary to do a sumifs

    thanks......that does help a little more.......

  19. #19
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Using scripting dictionary to do a sumifs

    Hello Welchs101,

    I use this technique a lot. Here is my version of the macro. It outputs the results on "Sheet3".
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by Leith Ross; 10-23-2014 at 10:18 PM.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

+ 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. need help with scripting dictionary
    By leanne2011 in forum Excel Programming / VBA / Macros
    Replies: 22
    Last Post: 10-04-2014, 04:33 PM
  2. [SOLVED] Using Dictionary in VBA scripting
    By vnzerem in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-04-2014, 02:54 PM
  3. Scripting Dictionary
    By Tendla in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-20-2013, 05:41 AM
  4. [SOLVED] Scripting Dictionary
    By thisisgerald in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 12-18-2012, 01:30 PM
  5. [SOLVED] Scripting Dictionary help
    By williams485 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-13-2012, 08:22 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