+ Reply to Thread
Results 1 to 13 of 13

Make loop run faster

  1. #1
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    Make loop run faster

    Hi all. I need to see if I can make this loop any faster, but first, I have a question regarding this. I don't know why I did

    Please Login or Register  to view this content.
    What does that mean? I just want to know when to use the "!" .. If I define any of those as an Integer or a Long, it rounds the final answer to a whole number...so I have to use the above in order for the loop to work correctly. Why is that?

    Anyway, here is the loop. If someone can tell me how to make it run faster, I'd really appreciate that. Thank you!

    Please Login or Register  to view this content.
    Last edited by Paul; 02-19-2010 at 11:58 PM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: making the loop run faster?

    Integer and Long are both whole numbers. But you can use Double if you need to allow decimal values.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    Re: making the loop run faster?

    so the, !, designates a variable as a Double?

  4. #4
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887

    Re: making the loop run faster?

    No, ! is a single. So as not to confuse users, just dimesion variables using words.
    Please Login or Register  to view this content.
    etc.

    There's also no reason to dimesion a variable as Integer, since it will be converted to Long automatically at runtime. Just dimension the variable as Long to start with.

    Also, please take a moment to read the forum rules and in the future wrap all code in between code tags like you see above.

    [code]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/code]

    Thank you.

  5. #5
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    Re: making the loop run faster?

    I did wrap everything as a code...?


    Can the loop above, in my first post, be made to run faster??

  6. #6
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    Re: making the loop run faster?

    IF I don't Dim anything, everything is treated as a Long? If I don't Dim anything, everything seems to work just fine. Could this cause problems?

  7. #7
    Registered User
    Join Date
    07-29-2009
    Location
    Los Angeles, CA
    MS-Off Ver
    Excel 2007
    Posts
    26

    Re: making the loop run faster?

    I have been doing this recently:

    Please Login or Register  to view this content.
    Did a search on "Optimize Slow VBA Code" and this is something I took from the first site that popped up.

    Currently, how long does it take?

    And why does EMAp = 10? I believe this variable may be your key to speed.

  8. #8
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887

    Re: making the loop run faster?

    Quote Originally Posted by losmi8 View Post
    IF I don't Dim anything, everything is treated as a Long? If I don't Dim anything, everything seems to work just fine. Could this cause problems?
    While your macros will execute and run normally, not Dim-ing variables means they will all be dimensioned as type Variant (not Long), which uses the most memory of any standard variable type since it can be used to contain any type of variable. It also can minutely slow down your code if there are a lot of variables that need their types determined. Dim all variables as their proper type is just good practice, and use a good naming convention.

    Along with declaring your variables, you may want to put Option Explicit at the beginning of all code/sheet modules. This will ensure that all variables must be Dim-ed, and that all variables are typed correctly within your code - if you accidentally type 'lastRow' in one area and 'lastRw' elsewhere, and you did not Dim a 'lastRw' variable, the procedure will not run and generate an error.

  9. #9
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    Re: making the loop run faster?

    Thank you Paul, very nice and convincing answer. I will do this from now!

    Justinvalle, it takes about 5 seconds to run (it does 5,000 rows). I know that isn't long at all, but that is just an example...I will be running 2-3 of those loops, then subtracting one from the other (so another loop). So I am concerned it will take just too long.

    I will try your code. I've seen that somewhere else but forgot to write it down. Thanks!!

  10. #10
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    Re: making the loop run faster?

    Also, I was wondering if there is a way of coding the loop differently that will make it run faster...or is what I am doing just fine?

  11. #11
    Registered User
    Join Date
    01-07-2010
    Location
    Dorothy and Todo Land, KS
    MS-Off Ver
    Office 2003 & 2007
    Posts
    34

    Re: making the loop run faster?

    To show others (or yourself) Excel hasn't frozen and the macro is running you can create a "please wait while the macro runs" form that appears while the macro runs.

  12. #12
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: making the loop run faster?

    What you're doing is fine if you understand it and it works the way it's supposed to every time...

    As for "can it be better", the answer is almost always yes. To get really good suggestions on how to do it better, post up a workbook showing sample data, and BEFORE/AFTER sheets so it's completely clear what you want to do.

    Different eyes looking at the same task may spot significantly more "efficient" approaches.

  13. #13
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887

    Re: making the loop run faster?

    Is EMAp always 10?
    If EMAp isn't always 10, can the value of EMAp be stored in a cell on the worksheet?
    Do you always start i = 2 to LR - EMAp?
    Do you always start j = 1 to EMAp?

    It might be best to provide a sample workbook with data. Also, can you explain, in words, what you're trying to accomplish with the For/Next loop(s)? It looks like you're trying to loop through cells in column G over and over and eventually coming up with a sum.

+ 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