+ Reply to Thread
Results 1 to 9 of 9

Code Efficiency [77 files, 1.04MM lines, 647MB]

  1. #1
    Registered User
    Join Date
    06-13-2012
    Location
    Dallas
    MS-Off Ver
    Excel 2007
    Posts
    8

    Code Efficiency [77 files, 1.04MM lines, 647MB]

    I'm getting started with VBA. I'm running Excel 2007. Looking to improve this code so that it might run faster. At current speeds it'll take 15+ hours (ran through 18MB out of 647MB in about 30 minutes).

    I have 77 different files I've renamed "1" through "77". Each file is set up as a data base with records in rows A through AF. For each record of each file I want to look in row N, and if it is a certain value add the entire record to a singular file. Here is the code I created. I believe there are efficiencies that could be implemented, I just don't know what they would be, being a newb. Any help would be appreciated. The code is below. Thanks!

    Please Login or Register  to view this content.
    Last edited by gwebb; 06-14-2012 at 11:32 AM.

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Code Efficiency [77 files, 1.04MM lines, 647MB]

    gwebb,

    Welcome to the forum!
    In the future, please wrap your code in code tags (link in my sig for how).
    As to your question, give this a try:
    Please Login or Register  to view this content.
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,329

    Re: Code Efficiency [77 files, 1.04MM lines, 647MB]

    Avoid selecting and activating anything. If you loop through the cells and build up one range variable using Union and run copy/paste that in one go for each workbook it will be faster. You can also use an autofilter and then just copy visible cells across.
    I'm on a phone at the moment but will try and post code later if someone else hasn't already jumped in.
    Everyone who confuses correlation and causation ends up dead.

  4. #4
    Registered User
    Join Date
    06-13-2012
    Location
    Dallas
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Code Efficiency [77 files, 1.04MM lines, 647MB]

    Thank you for the warm welcome.

    This looks to be a great start. As I go through the program piecewise it seems that after the autofilter is applied, once the line

    .AutoFilter 1, Array(3, 4, 9, 13, 15), xlFilterValues

    is executed all the lines in the 1File.xls are hidden but then not pasted in ThisWorkbook, and nothing particularly noticeable happens after

    .Offset(1).EntireRow.Copy wsDest.Cells(Rows.Count, "A").End(xlUp).Offset(1)

    is executed. Any thoughts?

  5. #5
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Code Efficiency [77 files, 1.04MM lines, 647MB]

    My apologies, made a mistake on the autofilter line. Each number should be surrounded in quotes "3", "4", etc. Here's the updated code:
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    06-13-2012
    Location
    Dallas
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Code Efficiency [77 files, 1.04MM lines, 647MB]

    Actually, in looking at it further I am having issues with the command line that is doing the pasting

    .Offset(1).EntireRow.Copy wsDest.Cells(Rows.Count, "A").End(xlUp).Offset(1)

    It seems to run fine until 1File is 10, then it pastes over previous data (it actually resets and pastes starting over at row 2, and there is an empty row after the data-- it's definitely writing/pasting an empty row, if that matters).

  7. #7
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Code Efficiency [77 files, 1.04MM lines, 647MB]

    The pasting an empty row is intentional, to avoid errors with specialcells (which is a method I'm not using). If you'd rather the empty row not be pasted, that can be amended, but generally speaking it shouldn't matter. Here is updated code so that the paste location is based off wsDest column N instead of wsDest column A:
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    06-13-2012
    Location
    Dallas
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Code Efficiency [77 files, 1.04MM lines, 647MB]

    This worked perfectly, can't thank you enough.

    I would like to ask for my own edification, given that I had to go through and rename 77 files for this procedure to work, what would be an example of some code that could change the names of the files in a given folder, or a For/Each Loop that could go through the files without renaming them, etc. Essentially, was this the cleanest way to go about this solution?

  9. #9
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Code Efficiency [77 files, 1.04MM lines, 647MB]

    gwebb,

    There are a couple ways to loop through each file in a folder, there's the Dir() method and the FileSystemObject (commonly shortened to FSO) method. I prefer FSO, and here's what it would look like:
    Please Login or Register  to view this content.
    Last edited by tigeravatar; 06-13-2012 at 05:03 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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