+ Reply to Thread
Results 1 to 9 of 9

Can someone help speed up my code?

  1. #1
    Registered User
    Join Date
    02-14-2013
    Location
    arlington
    MS-Off Ver
    Excel 2010
    Posts
    20

    Unhappy Can someone help speed up my code?

    I have a spreadsheet with travel information and I wrote code to loop through and find the specific columns I am looking for and then analyze them based on the criteria provided. I am relatively new to VBA and I am assuming there is a faster way to do this. Currently it takes about 69 seconds to execute. I dont want to use specific columns in case someone deletes a column that isnt needed. Any suggestions? Thanks

    Please Login or Register  to view this content.
    Last edited by Ppessina; 08-21-2013 at 04:27 PM.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,610

    Re: Can someone help speed up my code?

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    02-14-2013
    Location
    arlington
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Can someone help speed up my code?

    Sorry about that. Corrected

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Can someone help speed up my code?

    I have not look at the entire code, but what is .Rows().?
    Why can not you specify the row number? If you can not, I would use .usedrange, instead of rows().

  5. #5
    Registered User
    Join Date
    02-14-2013
    Location
    arlington
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Can someone help speed up my code?

    I am using .rows to find which row my text is in. I am trying to avoid putting row and column numbers in because the data contains a lot of misc info and if somone deletes a column it will cause errors. I am thinking maybe if the code is restructured and I am able to clear some of the memory this will speed things up but I am not sure how i would do it. I will try .usedrange in the mean time to see if this makes a difference.
    Last edited by Ppessina; 08-21-2013 at 05:00 PM.

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Can someone help speed up my code?

    You also have 10 nested loop. This obviously slows the code as it loop 10 times the number of rows.
    IMO, You would better-off by dividing the code in to 2-3 parts and linking each code by calling one another. It is easy to follow and adjust few many small codes than one massive and thick code.

  7. #7
    Registered User
    Join Date
    02-14-2013
    Location
    arlington
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Can someone help speed up my code?

    That is a good idea. I will try this as well. Thank you

  8. #8
    Valued Forum Contributor
    Join Date
    07-17-2005
    Location
    Abergavenny, Wales, UK
    MS-Off Ver
    XL2003, XL2007, XL2010, XL2013, XL2016
    Posts
    608

    Re: Can someone help speed up my code?

    Hi

    In addition to what has already been said, you are doing far too much reading and writing.
    Every time you read from the Sheet, and every time you write to the sheet you are incurring an enormous overhead in crossing the VBA / Sheet interface.
    You should read all of your data into an array.
    You should then dimension an output array of the appropriate size to take your result.
    All of the processing of the array should then happen within VBA, and only when you have your Output array, should it then be written back to the sheet with one single command.

    That would speed up your code by a huge magnitude.
    --
    Regards
    Roger Govier
    Microsoft Excel MVP

  9. #9
    Registered User
    Join Date
    02-14-2013
    Location
    arlington
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Can someone help speed up my code?

    Thank you for the information. Can you show me an example of how to do this with one of the loops in the above code? I am not sure how to tackle this. I have been researching it but still cant seem to get it. I broke the above code into 3 sections instead of one and it only sped up by 5 seconds so that did not do me any good.
    Last edited by Ppessina; 08-22-2013 at 12:33 AM.

+ 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] How to Speed Up the code ?
    By joh46k in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-03-2013, 09:42 PM
  2. Way to speed up VBA code
    By lalbatros in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-12-2007, 03:10 AM
  3. speed up my code?
    By mpeplow in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-18-2007, 09:07 PM
  4. [SOLVED] Speed up Code?
    By Sige in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-27-2005, 02:05 PM
  5. Speed up code
    By Derick Hughes in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-08-2005, 02:06 PM

Tags for this Thread

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