+ Reply to Thread
Results 1 to 28 of 28

more efficient way to do for-loop using application.worksheetfunction.countifs

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

    more efficient way to do for-loop using application.worksheetfunction.countifs

    Hi,

    I have a macro which takes ~ 46sec to run.

    In this file:
    Two sheets of data: Out_Sht, Query_sht

    I use a for-loop going through each of the LotID values on the Out_Sht using an application.worksheetfunction.countifs designation to count using specified criteria (see code).

    What i have works but i takes a really long time and i was wondering if there was a better way more efficient way.

    Any suggestions?

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

    Re: more efficient way to do for-loop using application.worksheetfunction.countifs

    dont want to influence anyone's suggestions but i was thinking a "scripting dic" approach might work but not sure how to do it.........i am kinda learning more and more about scripting dic-stuff but not sure how to handle that very complex countif and still use script dic.

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

    Re: more efficient way to do for-loop using application.worksheetfunction.countifs

    What exactly are you counting/calculating?

    How/why do you end up with more rows in the output than there is in the raw data?

    For example for LotID 3122-004 JBYAG there are 5 rows in the raw data and 22 in the output.
    If posting code please use code tags, see here.

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

    Re: more efficient way to do for-loop using application.worksheetfunction.countifs

    why more rows in output........long story too long to explain..........it suffices to say that the output worksheet comes from another file and i have to create extra cols of data for this file along specified guidelines...........

    what am i calculating:
    I am calculating two things (see enclosed code in file):

    First:
    Please Login or Register  to view this content.

    calculating value and putting into array:
    countif(Subj in out_sht is also in Query_sht, AND, DS value in Query_sht is less than or equal to DS value in out_sht, AND , Status col in Query_sht equals "Open")
    +
    countif(Subj in out_sht is also in Query_sht, AND, DS value in Query_sht equals "All", AND , Status col in Query_sht equals "Open")




    Second:
    output_answeredqueries_array(L1, 1) = Application.WorksheetFunction.CountIfs( _
    query_subj_range, output_subj_array(L1, 1), _
    query_segID_range, "<=" & output_datasegment_array(L1, 1), _
    query_qrystatus_range, "Answered") + _
    Application.WorksheetFunction.CountIfs( _
    query_subj_range, output_subj_array(L1, 1), _
    query_segID_range, "All", _
    query_qrystatus_range, "Answered")


    calculating value and putting into array:
    countif(Subj in out_sht is also in Query_sht, AND, DS value in Query_sht is less than or equal to DS value in out_sht, AND , Status col in Query_sht equals "Answered")
    +
    countif(Subj in out_sht is also in Query_sht, AND, DS value in Query_sht equals "All", AND , Status col in Query_sht equals "Answered")

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

    Re: more efficient way to do for-loop using application.worksheetfunction.countifs

    I've looked at the code and it isn't clear what you are counting.

    Could you explain in words?

    By the way, why are you looping through the output data?

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

    Re: more efficient way to do for-loop using application.worksheetfunction.countifs

    i posted what i am counting.........this is what i am counting.........not sure how else to explain...........

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

    Re: more efficient way to do for-loop using application.worksheetfunction.countifs

    anyone got any ideas?

    if you have any questions just let me know............

  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: more efficient way to do for-loop using application.worksheetfunction.countifs

    Where's the column subj?

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

    Re: more efficient way to do for-loop using application.worksheetfunction.countifs

    ahhhhhhhhhh so sorry.........

    LOTID is the same as SubjectID

    so subj is basicallyl the first col on each of teh sheets that contain data.

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

    Re: more efficient way to do for-loop using application.worksheetfunction.countifs

    Have you tried using formulas?

    For example.

    Please Login or Register  to view this content.
    That's still quite slow, because of the calculation, but faster then the loop.

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

    Re: more efficient way to do for-loop using application.worksheetfunction.countifs

    i was able to get it down from 43sec to 12sec........

    take a look at the code in the 2nd module.....the code in the first module contains the original code which takes 43sec.

    the code in the 2nd module does use a scripting dic ......but i still think it can be made faster........

    any thoughts or suggestions?

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

    Re: more efficient way to do for-loop using application.worksheetfunction.countifs

    anyone have any suggestions?

  13. #13
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: more efficient way to do for-loop using application.worksheetfunction.countifs

    Hi Welchs,
    try
    Please Login or Register  to view this content.

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

    Re: more efficient way to do for-loop using application.worksheetfunction.countifs

    wow........nilem, that is fast.....takes only 0.5 sec......

    i will have to review the code a bit to try and understand it but its a good learning experience.......

    very fast!!!!!!

    thanks!!!!!!!

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

    Re: more efficient way to do for-loop using application.worksheetfunction.countifs

    The code I posted that puts the formula directly in the cells takes 10 secs.

    But nilem's code is much better and shows the 'proper' way to use arrays for this sort of thing.

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

    Re: more efficient way to do for-loop using application.worksheetfunction.countifs

    norie, thanks for your help as well!

    nilem,
    in reviewing your code i am trying to understand what your doing here (see below):
    Please Login or Register  to view this content.
    also, what is "Array(i,i)"

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

    Re: more efficient way to do for-loop using application.worksheetfunction.countifs

    ok....i think i know what the answer was to my previous question

    'this adds/loads keys into dictionary
    'it also adds values to the dictionary
    'which corresponds to the rows of the duplicates
    'for example if 'junk' appears in rows 2 through 11
    'then the values in the dictionary for this key
    'is 2 and 11

    is this right?

  18. #18
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: more efficient way to do for-loop using application.worksheetfunction.countifs

    Yes, something like that.
    By the way, I think there is an extra line in my code: If y(j, 1) = x(i, 1) Then with the corresponding End If
    Try to remove it

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

    Re: more efficient way to do for-loop using application.worksheetfunction.countifs

    nilem,

    i was just going to as you about that line..........

    Please Login or Register  to view this content.

    so the line designated with the '**Question**

    this is not really needed......right?

  20. #20
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: more efficient way to do for-loop using application.worksheetfunction.countifs

    Yeah, right

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

    Re: more efficient way to do for-loop using application.worksheetfunction.countifs

    nilem,
    right now when i run the macro i get "empty" cells.........how do i make those empty cells zero's

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

    Re: more efficient way to do for-loop using application.worksheetfunction.countifs

    nilem,

    i added two lines of code.....

    Please Login or Register  to view this content.

    this does replace empty cells with 0's..........is this the right place?

  23. #23
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: more efficient way to do for-loop using application.worksheetfunction.countifs

    Hi David,
    try
    Please Login or Register  to view this content.

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

    Re: more efficient way to do for-loop using application.worksheetfunction.countifs

    ok...so i ran your new code and it works. I am just now trying to figure out why.

    ok....so as far as i can tell the only difference which would account for the "0's" being added is

    in the first macro you had:
    Please Login or Register  to view this content.
    in the 2nd macro you had:
    Please Login or Register  to view this content.

    but i dont get why the "&" would change anything........

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

    Re: more efficient way to do for-loop using application.worksheetfunction.countifs

    so is the ampersand just declaring the array as a type Long.............

    and is this why all the elements in the array are now not blank but 0

  26. #26
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: more efficient way to do for-loop using application.worksheetfunction.countifs

    David, I'm glad you're interested
    Try this to make it clearer
    Please Login or Register  to view this content.

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

    Re: more efficient way to do for-loop using application.worksheetfunction.countifs

    thanks.......i get it now.........

    hey, one more question......

    if i remove the "dim" statement for the variable "t" then i get a run time error........subscript out of range at this step in the code

    Please Login or Register  to view this content.
    if i leave the declaration statement
    Please Login or Register  to view this content.
    the code runs fine any ideas?

  28. #28
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: more efficient way to do for-loop using application.worksheetfunction.countifs

    But why do you remove a variable from a string variable declarations? If you use the Option Explicit operator then your code will not work against such action.

+ 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] worksheetfunction countifs, counting down and across, application defined error
    By Lifeseeker in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-16-2012, 11:58 AM
  2. worksheetfunction.countifs with loop from multiple worksheets
    By Lifeseeker in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-02-2012, 04:55 PM
  3. worksheetfunction.countifs with loop
    By Lifeseeker in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-02-2012, 01:26 PM
  4. Replies: 0
    Last Post: 12-16-2011, 09:01 AM
  5. error trying to use "Application.WorksheetFunction.Sum(CountIfs"....in vba
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-20-2011, 09:15 PM

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