+ Reply to Thread
Results 1 to 19 of 19

Make code faster/better best practices

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

    Make code faster/better best practices

    Hi all: I have the following section of code and i am trying to make it faster. Currently when this procedure runs the screen "changes" durings its execution. Now i know i can "turn off screen updating" and this will increase speed but are there commands in the code which will prevent the screen from updating. I know activating or selecting things causes the screen to change. Also, any inputs to make this faster/better would be greatly appreciated.

    Please Login or Register  to view this content.
    Last edited by welchs101; 12-29-2008 at 10:17 PM.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464
    Hi,

    There is a small time overhead involved every time you jump backwards and forwards to the Excel Applications from the VBE and vice versa. So depending on the number of cells you're processing, it might be better to read all the cell values into a VBA array, process them entirely within the VBE and then finally write the results back to the Excel App.

    HTH

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

    Just to clarify, did you mean to read BOTH lists into individual arrays.........and process them using the arrays.

    David

  4. #4
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712
    Anyone have a good / fast way to compare to arrays ?

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Maybe as attached. The lists of old and new students should be culled for duplicates and trimmed before running.

    If you try several methods, I'd be curious as know the timing results.

    You could also just use two columns of formulas ...
    Attached Files Attached Files
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712
    thanks SHG............ i will check it out.

    DAvid

  7. #7
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    This is what I used to speed things up.
    http://vbaexpress.com/kb/getarticle.php?kb_id=1035

  8. #8
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712
    Thanks SHG. I am still reviewing the code you sent. My skill level not quite the same as yours but i am good enough to review it and learn some things. So thanks.

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    A couple of changes to ignore case in name comparisons, and to avoid an error if the number of adds or drops is zero:
    Please Login or Register  to view this content.
    Last edited by shg; 12-29-2008 at 09:20 PM. Reason: correct compare operator

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

    Found an error.......

    Hi SHG,

    I think i found an error in the code.

    Recall the following section of code:

    Please Login or Register  to view this content.

    I think this should actually be the following (no equal sign)

    Please Login or Register  to view this content.

    I have not checked your latest code but i think it still has this issue. I will check your new code now. also, i modified it slightly so that the inputs to the procedure are the same as my old one.

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

    In reviewing your code more closely i realized that the "sort" was very important. Without it the procedure would not work correctly. If i sort the ranges as is then data would not align correctly as beside the names are grades and if sort just the names then the grades would not line-up with the right names.

    I really like your code though. Its very compact and i learned something from it.

    the procedure i currently use has the following inputs:

    Please Login or Register  to view this content.
    so this is what i am working with. i cant use your code unless i sort all rows in the given range. what i wonder is if i should read each range into an array and compare just the arrays? suggestions.

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

    This is what i came up with .....

    hi Shg, below is the code i came up with based on your helpful inputs. I learned alot.

    Please Login or Register  to view this content.

  13. #13
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    You should compare performance of the two approaches with perhaps 25,000 names.

    Redim Preserve is an expensive operation; that's why I resized the arrays to their max possible size at the outset.

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

    Sure i can do that. How do i calculate the time for a procedure to run? Do you know an easy way?

    David

  15. #15
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Application.Timer returns the number of seconds since midnight, including fractional seconds on Windows (whole seconds only on Mac).

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

    I ran two tests. One using your program and one using mine. I used the same names that were in your original spread sheet. I think the first list had ~1688 entries and the 2nd has ~ 1679 entries.

    Yours took ~ 0.281 sec
    Mine took ~ 39sec

    Thats a BIGGGGGGGGGGGGG difference.

  17. #17
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Mark as solved?

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

    Yours is definitely faster. Thanks.

    Yes i will mark as solved.

  19. #19
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    You're welcome.

    Both Richard's and Kenneth's suggestions certainly are valid as well. Regarding Richard's, my code would probably be faster by reading both lists into memory. Regarding Kenneth's, all his suggestions are good if the code is changing cell contents incrementally like a lot of code does; this just happens not to, but you should internalize his example for the future.

+ 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