+ Reply to Thread
Results 1 to 6 of 6

Performance of VBA

  1. #1
    Registered User
    Join Date
    05-06-2007
    Posts
    20

    Performance of VBA

    Are there any information how much different statements require CPU time?
    What is for instance difference between While and For Next loop. Which one is more effective Select Case or IF. What is the fastest way to step counters etc.
    Leevi

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    I have never seen any comparsions.

    From experience it is all in the way you code & what you are doing within the loop

    Personally I prefer to use For loops as they have a counter built into the command as Do & While loops you need to add a seperate command to add 1 to the counter variable

    For tips on increasing macro speed have a look at
    HTML Code: 
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  3. #3
    Registered User
    Join Date
    05-06-2007
    Posts
    20

    for next vs. while do

    I made a simple test program where I compared execution times:
    For loop took ~20s and the corresponding while loop ~25s => While loop is 25% slower.
    Leevi

  4. #4
    Forum Contributor Tarball's Avatar
    Join Date
    04-21-2007
    Location
    Atlanta, GA
    Posts
    166
    You should post the exact statements that you ran. I would suspect that you could increase/decrease performace of either statement by simply not type declaring your variables, or using a counter vs not using a counter. Also, what you do inside the loop would make a huge difference too; even using boolean logic will be faster than using comparisons with the '=' operator.

    There is a great MS-Excel book that dives into this very topic in one of its chapters. In addition, the books focus is on professional development of MS-Excel applications and provides a deep core understanding of the tool.

    "Professional Excel Development: The Definitive Guide to Developing Applications Using Microsoft Excel and VBA"

    Authors: Stephen Bullen, Rob Bovey, and John Green

    An excellent book and reference.
    Reach me at excel_help at bellsouth dot net

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    I second Tarball's recommendation of Professional Excel Development, with the caveat that a lot of it (most of it) is not for beginners. They have a ton of information on best practices.

    1. In terms of speed, iterating elements from a collection (For Each/Next) is fastest, followed by For/Next and Do/Loop. I would always use For/Next rather than Do/Loop unless there was a good reason not to; the code is usually easier to follow.

    2. Select Case statements are prettier, If/ElseIf/EndIf statements are faster (curious; you'd think the compiled code would be the same). Either should be arranged to test cases from most likely to least.

    3. Don't use Option Compare Text unless you have no other way.

    4. Don't use wildcard comparisons (If s Like "*##*" Then) unless you have to.

    5. The Len function is among Excel's fastest; all it has to do is look at the first word of a string, where the length is stored. So rather than If s = "", use If Len(s) = 0

    6. Declare all variables appropriate to data type. Use Variants only when necessary.

    7. Use early binding for all Objects unless you have specific reason not to.

    8. Write logical, clear code first, and then optimize for speed. Obscuring the structure and intent of code for speed will have the person that has to maintain it later casting aspersions on your lineage.

  6. #6
    Registered User
    Join Date
    05-06-2007
    Posts
    20

    test code

    Please Login or Register  to view this content.
    Leevi

+ 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