+ Reply to Thread
Results 1 to 12 of 12

Why is code in .xlsm slower than .xls in excel 2007

  1. #1
    Forum Contributor
    Join Date
    04-30-2009
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    496

    Why is code in .xlsm slower than .xls in excel 2007

    Hello all. I need to know what I can do to speed up my code. The following code runs in 12 seconds if the file is .xls but when converted to xlsm, it takes 5 min and 10 seconds to process 18 columns and 12 rows of data.

    Please Login or Register  to view this content.
    Thanks,
    Andrew
    Last edited by drewship; 08-31-2010 at 10:05 AM.

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Why is code in .xlsm slower than .xls in excel 2007

    As posted, that code should not compile or run, in any version of Excel...
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Why is code in .xlsm slower than .xls in excel 2007

    If the same code runs so much faster in Excel 2003 then I'd suspect that, at some point, it's looping through all of the rows on the sheet - and it's a lot quicker to loop through 65,000 rows than it is to go through 1,000,000 rows.

  4. #4
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Why is code in .xlsm slower than .xls in excel 2007

    The compile problem seems to be caused by some extra & and " that you don't need when constructing file paths and names.

    I notice you're using select a few times throughout the code. This is almost never necessary and can really slow things down.

    Other than that I'd pop a few break points in your code and see which bits are causing the bottlenecks.

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Why is code in .xlsm slower than .xls in excel 2007

    You also refer to a non-existent Col() property
    It should be faster to use:
    Please Login or Register  to view this content.
    rather than replacing cell by cell, and your use of xlDown rather than xlUp could cause what Andrew mentioned (i.e. looping through every single row).

  6. #6
    Forum Contributor
    Join Date
    04-30-2009
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    496

    Re: Why is code in .xlsm slower than .xls in excel 2007

    Thanks for the replies. It appears I did not edit the posted code correctly when I tried to change from a server location to the C drive. I think that is corrected below. I will try to make the changes everyone indicated but I am not sure they address the overall question of why .xlsm is slower than .xls. Hopefully they will correct this issue since I would rather use .xlsm files. I have many other files that are affected as well and they are much larger files so this is not an isolated instance.

    Please Login or Register  to view this content.
    Thanks,
    Andrew

  7. #7
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Why is code in .xlsm slower than .xls in excel 2007

    I think the problem is at the start of the DeleteBlankRows and DeleteBlankColumns routines:

    Please Login or Register  to view this content.
    In the first line of the sub you're selecting all of the cells on the sheet, then you're setting the range to loop through to all of the selection.

    So this will evaluate more than 16,000 columns (compared to 256 in Excel 2003) and more than 1,000,000 rows (compared to 65,000) - hardly surprising it's slower.

  8. #8
    Forum Contributor
    Join Date
    04-30-2009
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    496

    Re: Why is code in .xlsm slower than .xls in excel 2007

    Romperstomper, I am not sure what Col() properity is non-existant but I am trying to use your code and I get "Expected: end of statement" with "XS" hilighted.

    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    04-30-2009
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    496

    Re: Why is code in .xlsm slower than .xls in excel 2007

    Thanks Andrew-R...I can see where this may slow things down and will change it to see the effect.

    Andrew

  10. #10
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Why is code in .xlsm slower than .xls in excel 2007

    You don't need to loop through the cells using replace. It would be more like:

    Please Login or Register  to view this content.

    Dom

  11. #11
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Why is code in .xlsm slower than .xls in excel 2007

    Quote Originally Posted by drewship View Post
    Romperstomper, I am not sure what Col() properity is non-existant
    in your DeleteBlankColumns sub you have this:
    Please Login or Register  to view this content.
    but there is no Col() property.
    I am trying to use your code and I get "Expected: end of statement" with "XS" hilighted.

    Please Login or Register  to view this content.
    that's not my code. There is no loop in my code - it's just inside the With Sheets...End With block:
    Please Login or Register  to view this content.

  12. #12
    Forum Contributor
    Join Date
    04-30-2009
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    496

    Re: Why is code in .xlsm slower than .xls in excel 2007

    Sorry romperstomper and Domski...I was distracted and sent before I could proofread and seperate my questions. I wasn't sure how to make the change in the code so I tried a couple things but never removed the loop. I incorporated both your code and limited the rows and columns being processed that Andrew-R indicated could be a bottleneck. I also removed the unreferenced Col() and Row() statements with no ill effect and now the code processes in less than a second in .xlsm!! Thanks everyone!!

    Andrew
    Last edited by drewship; 08-31-2010 at 10:09 AM.

+ 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