+ Reply to Thread
Results 1 to 8 of 8

Code works but takes hours to run.

  1. #1
    Registered User
    Join Date
    09-07-2012
    Location
    Des Moines, Iowa
    MS-Off Ver
    Excel 2003
    Posts
    5

    Code works but takes hours to run.

    I have data on one wokr sheet that is in 4 columns, Item, qty, and 2 date columns. The code I have written loops through this data, ( about 70K rows), combines items and dates that are similar. This is moved to a different sheet that is set up with items in column 1 starting at row 20 ( over 9,000) and dates starting from cells e20 through e76. My code finds the correct date then looks in column 1 for the correct item and once found enters the value stored form the previous sheet. As I said the code works fine but by using the "do until loops" the code takes several hours to run. I know there is a faster way to do it but I have not been able to find one that will work. Any suggestions would be appreciated.

    this is an example of the spread sheet I am moving data to

    Item Desc WC Type Type 9/1/2013 9/8/2013 9/15/2013
    12345 xxxxx bbbbb cas
    12346 xxxxx bbbbb cas
    12347 xxxxx bbbbb cas
    12348 xxxxx bbbbb cas
    12349 xxxxx bbbbb cas
    12350 xxxxx bbbbb cas
    12351 xxxxx bbbbb cas
    12352 xxxxx bbbbb cas


    This is the code that takes hours to run

    Please Login or Register  to view this content.

  2. #2
    Forum Contributor
    Join Date
    09-11-2013
    Location
    Huntsville, AL
    MS-Off Ver
    Excel 2007
    Posts
    180

    Re: Code works but takes hours to run.

    Why not use a pivot table?

  3. #3
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Code works but takes hours to run.

    Hello mikemarsha,

    Welcome to the Forum.

    Have you tried to turn Screenupdating off and on, and the same with Calculation from manual to Automatic?

    If you had posted your complete Code, I could have added it for you, which could have made a difference.

    Regards
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Code works but takes hours to run.

    I don't know if this is the primary problem but you almost never need to (or want to) activate and select things to operate on them. It adds a lot of extra useless work and will slow down execution. Also I suspect you are not showing enough code for a full diagnosis. This is because you have an unmatched Loop statement at the end. What is going on in that outer loop? Can you attach your file? Or at a minimum post all the code?

    It might help if you explained your logic but I'm guessing you don't need that If statement. The loop will exit the first time that the cell equals a at the end of the loop. Which means the If test is always going to be True, after the first pass through the loop. Is that what you intended? I'm reading this as you want to look for the first cell after the active cell that is equal to a or blank, and replace the value with b. That's what this version does:

    Please Login or Register  to view this content.
    If instead you want to look for the first cell including the initial active cell with a match, I would suggest you flip your conditions and use While:
    Please Login or Register  to view this content.
    These changes would speed things up noticeably but I don't think it will get you from hours to seconds.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  5. #5
    Registered User
    Join Date
    09-07-2012
    Location
    Des Moines, Iowa
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Code works but takes hours to run.

    I have to be abel to modify the data later after its loaded in the table.

  6. #6
    Registered User
    Join Date
    09-07-2012
    Location
    Des Moines, Iowa
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Code works but takes hours to run.

    Here is the complete code for the sub. 1st I load my variables a = my item number, b = the qty and c = the week start date. ( I have the variable set to nothing to begin with becuase I will loop through this entire process and when I start the loop again I want my varibles emptied out). I then move to the 1st tab and find the date that matches variable c in row 20. At that point my code looks for the cell in column a that has the item number that matches variable a. When it finds that item I load variable b into the empty cell were they intersect. My if statement is basically moving my activecell down the column my date was found in until it finds that correct item in column a. Thats a total of 9,194 cells it may have to check which is why it takes so long to run.

    Please Login or Register  to view this content.
    Last edited by mikemarsha; 09-28-2013 at 12:32 PM.

  7. #7
    Registered User
    Join Date
    09-07-2012
    Location
    Des Moines, Iowa
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Code works but takes hours to run.

    I am going to play with the code you have here. It moves very quickly but it does not find the match to variabkle a in column a. Thanks for the reply it gives me some ideas to work with.

  8. #8
    Registered User
    Join Date
    09-07-2012
    Location
    Des Moines, Iowa
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Code works but takes hours to run.

    I had not done that but will, thanks you for the advice

+ 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] My formatting code takes forever - please enhance?
    By nickmax1 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-10-2013, 06:33 AM
  2. [SOLVED] Formula that takes takes out a specific word/number from text string
    By ncalvelo in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 11-29-2012, 10:21 AM
  3. Long code takes an age to run
    By E3iron in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-05-2009, 07:38 AM
  4. My Code takes too long to execute
    By coreytroy in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-30-2008, 12:12 AM
  5. [SOLVED] Excel and Works should add hours and minutes over 24, no seconds
    By Upward Bound in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-22-2005, 11:25 PM

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