+ Reply to Thread
Results 1 to 17 of 17

which option faster and why?

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    which option faster and why?

    I have a column of names which i am trying to parse.

    1) The first ways is to go through each cell in the column and parse it accordingly. Here is the code i used to parse the col of strings.

    Please Login or Register  to view this content.




    2) the 2nd way is to insert a col next to the column of interest. insert "excel formulas" into this new col which modify the previous col entries. Next fill down formulas, cut-n-paste values into previous col, then del col you added.





    Which way is faster? The 2nd way................go figure.......would never have guessed it.

    I have a macro file that is about 1.3Meg in size. When i run this macro and try to run option 1 it takes about 10 sec as compared to about 1.8 secs with the 2nd option.


    note: If i just want to parse the col of strings and I DONT have this 1.3Meg file open......ie...just parse file using a test file.........it takes no time at all for either option to run. But for some reason option 2 is fastest when i have this 1.3meg macro file open.

    any thoughts?
    Last edited by welchs101; 06-09-2011 at 05:27 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: which option faster and why?

    Hello Welchs101,

    The VBA method is slower for a couple of reasons. First, VBA has to check code for correctness, compile it, load it into memory, and then run it. Once that is all done, the code will execute more quickly, e.g. the second time you run it, provided you haven't made any changes to the code. For VBA to gain access to the worksheet objects it must go through several more thread layers of the Excel application before it can access the object.

    The worksheet formulas have the advantage of already being compiled and not having the extra layers of indirection to access the objects. This speeds up the code execution considerably.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: which option faster and why?

    thanks for the info.

    But, why does having the 1.3Meg file open change how fast the VBA program runs. Its almost instantaneous when the 1.3Meg file is closed.

  4. #4
    Registered User
    Join Date
    05-11-2011
    Location
    College Station, Texas
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    42

    Re: which option faster and why?

    Hi,

    In option1, try adding these two lines at the beginning of your vba code and see if it makes the code run faster:

    Application.ScreenUpdating=False
    Application.EnableEvents = False

    Thanks and Best Regards,
    Abi

  5. #5
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: which option faster and why?

    With Option 2 you don't need to loop through the rows and, by the sounds of it, you could have a lot of them.
    Hope that helps,

    Colin

    RAD Excel Blog

  6. #6
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: which option faster and why?

    ok.

    I have two files. One is an excel file that is 1.3Meg in size. It has many sheets and macros in it.

    The other file is basically a subset of the first file. I have copied the code this code


    Please Login or Register  to view this content.



    into the file and i run this under two circumstances.


    1) The 1.3Meg file is not open. Run time is instantaneous....miliseconds.
    2) With the 1.3Meg file open. Run time is several seconds....like 25 secs to run...not kidding on the time.


    Adding the code below did nothing to change the time....which makes sense the screen does not change when its running anyway.

    Application.ScreenUpdating=False
    Application.EnableEvents = False


    very strange..................any ideas?

  7. #7
    Valued Forum Contributor
    Join Date
    07-21-2008
    Location
    London, UK
    Posts
    326

    Re: which option faster and why?

    screen updating on / off stops the flickering

    Its considered quicker as a result

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

    Re: which option faster and why?

    welchs101,

    I would say its because of the number of operations that the macro is writing to the sheet. In option 1, each cell is being written to individually. In option 2, a column is inserted, the formula is written, then copied down (the copy down is only one operation), then that range is copy/pastespecial (as values), then the column deleted. So only about 5 or 6 operations compared to an operation in every single cell.

    To speed up option one, you could make it so that the names are updated within the macro and all put down at once instead of one at a time, resulting in only one operation to the sheet. The following code will do that:
    Please Login or Register  to view this content.


    That should speed up the macro a bit

    Hope that helps,
    ~tigeravatar

  9. #9
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: which option faster and why?

    thanks all for the info.

    tigeravatar, i will try your new code and let you know what i find out.

    I do think its strange that its instantenous without the 1.3meg file open and takes 30sec or more with the file open. Note: This 1.3meg file has nothing to do with the actual macro....its just open and sitting there.

    Thanks again to all who responded.

  10. #10
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,692

    Re: which option faster and why?

    What kind of formulas do you have in the 1.3 meg file?

  11. #11
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: which option faster and why?

    the ones i can think of are

    1) countifs
    2) sumifs
    3) match and index
    4) lots of vlookups

    Why did you ask this question?

  12. #12
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: which option faster and why?

    tigeravatar,

    I just ran the code you posted in #8 and that ran "instantaneously"............having the 1.3meg file open or closed had no impact. This is what i am looking for.

    So exactly what was i doing different in the other code?

    So i think your reading all the values into an array........is this right?

    Then your putting all the values from the array into the sheet..........is this right?


    Also, i dont understand this


    Please Login or Register  to view this content.

    i think this is somehow putting the array into the sheet but i dont know how. can you explain briefly.

    THANKS AGAIN!

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

    Re: which option faster and why?

    Quote Originally Posted by welchs101 View Post
    So i think your reading all the values into an array........is this right?
    Yes, this is correct. All of the items in column A are loaded into the array named arrProducts. The code to get the trimmed, lefthand portion of the item is then operated for each item within the array without writing it to the sheet yet. Internal operations like this are much faster than external operations (those that write to the sheet).

    Quote Originally Posted by welchs101 View Post
    Then your putting all the values from the array into the sheet..........is this right?
    Yes, at the end of the code (explained in the next point) it ouputs the entire array, all at once, to the sheet so that only a single external operation is performed.

    Quote Originally Posted by welchs101 View Post
    Also, i dont understand this


    Please Login or Register  to view this content.

    i think this is somehow putting the array into the sheet but i dont know how. can you explain briefly.
    The items started in A2 and need to be returned to A2 (and down) because the desired outcome of the macro is to replace existing data with the cleaned data. In order to do that, the code "selects" A2 (its not really a selection, it just uses that as a starting point), and then resizes the range its writing to to be a number of rows equal to the number of items in the array. Now that the range to output to is the correct size, it dumps the array data into that range.

    Hope that helps,
    ~tigeravatar

  14. #14
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: which option faster and why?

    thanks!

    What would happen if you did not have the "resize" operation in the code. What if you had just

    Please Login or Register  to view this content.

    Why do you need the resize?

    thanks again

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

    Re: which option faster and why?

    welchs101,

    Best way to learn is from first-hand experience, so you should give that suggestion a try and see what the results are.

    However, what will happen is that only A2 will receive the first value of the array. And then, with no more cells to populate, nothing else will happen. That's why you need the range.resize operation.

    ~tigeravatar

  16. #16
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: which option faster and why?

    thanks for your help!

  17. #17
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: which option faster and why?

    And what using :

    Please Login or Register  to view this content.
    or

    Please Login or Register  to view this content.
    or

    Please Login or Register  to view this content.
    Last edited by snb; 06-09-2011 at 04:17 PM.



+ 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