Consulting

Results 1 to 19 of 19

Thread: Need help in accelerating a process(too many loops)

  1. #1

    Lightbulb Need help in accelerating a process(too many loops)

    Hello everyone,

    I have been given 2 macros to proceed a process everyday. They work independently. There's no bug in the code. The code is commented.

    However, it takes too much time when running(an hour in total, and it blocks the computer sometimes). I've been told that there are some unnecessary loops in each piece of code that speed down the process. Unfortunately, I'm really not specialised in VBA. So I hoped you guys could give me some insight in simplifying the code.

    CHECK GAP ANALYSIS.xlsb
    CHECK REORG.xlsb

    The instructions of the process are explained in detail in the first sheet of each file.

    Thank you in advance.

    Regards,
    Francis
    Last edited by FrancisZheng; 02-23-2017 at 03:56 AM.

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Way, way, way too many Select...Selections.
    It is boring, tedious, basic beginner "Gee, what's a macro" noobie work to eliminate those.


    A total lack of With ... End With'es
    It is boring, tedious, basic beginner noobie work to eliminate those.


    Way, way too many Active Whatever'es.
    It is boring, tedious, interested party, getting better, but still a noob, work to eliminate those.





    Fixing those three issues should improve the speed by 30% - 50%
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Hi Sam,

    I'll try to reduce the frequency of Select and Active and replace them with With instead. Are Select and Active really so slow that I can speed up by 30%-50%?

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    There's a lot of code in the 2 workbooks

    Which part runs slowly?
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    While not technically true, think of it this way

    Workbooks("XYZ").Sheets("Sheet1").Range("a1)).Font.Bold = True
    Requires the computer to read the hard drive and load the Workbook into memory, Look at Sheet1, Range A1, Then refer to the Font Object of that Range and set the Bold Property. Then it will unload all that from memory.

    If the Next line merely changes the Font Size to 10, it must repeat all that. Again
    Next, Change the Font.Name. Repeat again, all over again.

    This is virtually three time faster.
    With Workbooks("XYZ").Sheets("Sheet1").Range("a1").Font.
    .Bold = True
    .Size = 10
    .Name = "Times New Roman"
    'For every reference to Font, remove yet another load into and unload from memory
    End With
    While eliminating Select... Selection's doesn't give as drastic an advantage, it is still worthwhile, especially in loops, where the speed up is present in every iteration of the loop.



    When speed is a concern, setting frequently accessed Objects to Variables helps because, once set, they stay in memory until the Sub is Ended.

    For example, suppose you refer to Workbooks("XYZ") and several of its Sheets
    Set Masterbook = Workbooks("XYZ.xlsx")
    Masterbook.Sheets(1)
    Masterbook.Sheets(2)
    Is better

    Referring to sheet1 even just twice
    Set Sht1 = Masterbook.Sheets("Sheet1")
    Is faster

    The only time an Object Variable is not faster is when the code in a sub only refers to the Object one time.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  6. #6
    Hi Paul,

    The piece of code that runs the slowest would be the file "CHECK REORG", in which both the import and the analysis are quite slow.

    Plus, when I import the no.5 file : ".2016.12.21 (Post-UAT4) GBIS7 INT2 SUF - ALL.xlsx", if I import the file in .xlsb(which saves space), it bugs. However, I can import the other files in .xlsb with no problem. Do you know what the reason would be?

    Francis

  7. #7
    Thank you Sam.

    I had no idea how Excel proceeds when compiling a code. I've learnt a lot. I will try to simplify the way you did.


    Francis

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Quote Originally Posted by FrancisZheng View Post
    Hi Paul,

    The piece of code that runs the slowest would be the file "CHECK REORG", in which both the import and the analysis are quite slow.

    Plus, when I import the no.5 file : ".2016.12.21 (Post-UAT4) GBIS7 INT2 SUF - ALL.xlsx", if I import the file in .xlsb(which saves space), it bugs. However, I can import the other files in .xlsb with no problem. Do you know what the reason would be?

    Francis
    What I meant was "Is there / Are there particular subs that run too slow?"

    That XLSB has hundreds of lines of code and dozens of subs/functions and since there is no test data, any thoughts would have to be by Desk Check
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  9. #9
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    When you import an xlsx as an xlsm, Excel first converts the file, then opens it. Then when you save the xlsb as xlsx, Excel has to convert it back before writing to disk.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  10. #10
    What I meant was "Is there / Are there particular subs that run too slow?"

    That XLSB has hundreds of lines of code and dozens of subs/functions and since there is no test data, any thoughts would have to be by Desk Check
    I think the sub "ReformatFPV" and "AddStatus" in the module "Action_TREEVIEW" are the ones that run the slowest.

    Francis.

  11. #11
    When you import an xlsx as an xlsm, Excel first converts the file, then opens it. Then when you save the xlsb as xlsx, Excel has to convert it back before writing to disk.
    Is there a default file type when importing files? I don't know why the type .xlsb works for the other files when importing.

  12. #12
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Have you got a copy of the code that you have "cleaned up?"

    I don't know why the type .xlsb works for the other files when importing.
    No Code in them? Maybe?
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  13. #13
    Hi Sam,

    I didn't work during the weekend. I'm still trying to "clean up" the code of "CHECK REORG" by adding as many "with"s as possible.

    Francis

  14. #14
    Got it

  15. #15
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Gonna Share?
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  16. #16
    Sorry Sam, I thought I got it. It turned out that the code got faster because some procedures were skipped because of "On Error Resume Next"...

  17. #17
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    There is a timer example here for accurately testing the speed of code elements
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  18. #18
    Thank you. I'm looking into that.

  19. #19
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Well? Let's see what you have now.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •