+ Reply to Thread
Results 1 to 18 of 18

Make a macro more efficient/faster

  1. #1
    Registered User
    Join Date
    06-18-2014
    Location
    Netherlands
    MS-Off Ver
    2014
    Posts
    23

    Make a macro more efficient/faster

    I am using a macro that when you press a button, it searches in column F for 2410 and hides it when he sees that value. Same with 2510. Only problem is, it takes like 5 min if it has to go through a database of 1000 rows. Anyone got a good suggestion how I can make this faster or have a better idea for a macro?

    I attached an example, which only shows a small proportion. I also need to add a macro like that for the columns, could use the same, but would be great if someone can teach me a better way.

    Please Login or Register  to view this content.
    edit: I can't change the merged cells in the file, I know they are just crap, but it's a must and can't do anything about it.
    Attached Files Attached Files
    Last edited by jhonneyboy; 06-25-2014 at 03:17 PM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,514

    Re: Make a macro more efficient/faster

    Please Login or Register  to view this content.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Contributor
    Join Date
    01-30-2014
    Location
    England
    MS-Off Ver
    MS Office 2010, 2007, 2003
    Posts
    149

    Re: Make a macro more efficient/faster

    Hi jhonneyboy

    Try adding this to the beginning:
    Please Login or Register  to view this content.
    And this at the end:
    Please Login or Register  to view this content.
    Stops the screen flashing and showing what the computer is currently doing

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,514

    Re: Make a macro more efficient/faster

    Or, better:

    Please Login or Register  to view this content.

    Regards, TMS

  5. #5
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Make a macro more efficient/faster

    Edit: Got beaten to it by the ones above, the first bit of code is similar to the above but with the .specialcells construct added, I'm not sure that will actually make much difference speed-wise unless you are doing 100000s of rows.
    Please Login or Register  to view this content.
    The other thing to do would be to use a find...find next construct rather than checking everyrow but I don't think it will make much difference based on what your data looks like.

    The quickest way would probably be to use a formula in a hidden column to work out which rows should be hidden and then filter on the values returned by the formula. I've uploaded an example of this.

  6. #6
    Valued Forum Contributor Miraun's Avatar
    Join Date
    04-03-2009
    Location
    New England
    MS-Off Ver
    2003, 2007, 2010, 2013
    Posts
    554

    Re: Make a macro more efficient/faster

    Hi JHonneyboy;

    I've taken a look at the code and done a couple quick optimization changes.

    Please Login or Register  to view this content.
    The first, and most substantial, is starting with application.screenupdating =False, and then changing it back to true at the conclusion. Otherwise, it's going to constantly calculate and update what the layout is going to be to the end user throughout every hide/unhide event.

    Secondly, I removed the with statement and consolidated that down into a single line. I'm not sure if it's necessary, or really adding too much power, but typically I try to avoid using With statements if it's going to be effecting a single line.

    Lastly, I removed the clause at the conclusion to set rng = nothing. Since the variable is Dim'ed within the Sub, and not set as a public variable, the variable will not persist beyond the sub's execution. Therefore, no reason to clear it out at completion. This is not really an efficiency gain, as it's like, a billionth of a nanosecond for excel to handle setting the variable... just something I'd let you know about.

    Give that a shot and see if it still meets all of your expectations and needs. And if not, let me know!
    Going for Guru! Click the Star to the bottom left of this post if I helped!

  7. #7
    Valued Forum Contributor Miraun's Avatar
    Join Date
    04-03-2009
    Location
    New England
    MS-Off Ver
    2003, 2007, 2010, 2013
    Posts
    554

    Re: Make a macro more efficient/faster

    Geeze... competition is fierce on the contributor side today...

  8. #8
    Forum Contributor
    Join Date
    01-30-2014
    Location
    England
    MS-Off Ver
    MS Office 2010, 2007, 2003
    Posts
    149

    Re: Make a macro more efficient/faster

    It's the heat

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,514

    Re: Make a macro more efficient/faster

    Please Login or Register  to view this content.
    This line of code, and the corresponding End If is redundant, given that you have set it to a fixed range on the line before testing it.

    Regards, TMS

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,514

    Re: Make a macro more efficient/faster

    @Miraun: I don't think your code/oprimisation works. The r.MergeArea is important when linked to the .Cells(1) check

    Regards, TMS

  11. #11
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Make a macro more efficient/faster

    Not sure, if you are referring to my post in particular TMS but good spot, I should have done:
    Please Login or Register  to view this content.
    Having put a bit more thought into it. I quite like the solution in the attachment in post 5 though, I think that is a bit smoother.

  12. #12
    Valued Forum Contributor Miraun's Avatar
    Join Date
    04-03-2009
    Location
    New England
    MS-Off Ver
    2003, 2007, 2010, 2013
    Posts
    554

    Re: Make a macro more efficient/faster

    You're right... Didn't get any syntax errors when running, but just noted that it wasn't fully hiding the non-2410/2510 values... Fixed it would be:
    Please Login or Register  to view this content.

  13. #13
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,514

    Re: Make a macro more efficient/faster

    @ragulduy: I haven't tested your code. My point was related to Miraun's code, which doesn't work. r.MergeArea.Cells(1).Value isn't the same as r.Value

    Please Login or Register  to view this content.

    Regards, TMS

  14. #14
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Make a macro more efficient/faster

    I meant post #9 haha, can't keep up.

    Edit: Just re-read things, I had thought there was a test to see if the cell value was blank before running the hide command, which I saw as being redundant as I was setting rng to the non-blank cells. Having re-read the code though, not the case, so just ignore me!
    Last edited by ragulduy; 06-25-2014 at 11:50 AM.

  15. #15
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,514

    Re: Make a macro more efficient/faster

    @ragulduy: no, it was a general observation to the OP, and anyone who is interested, that that line of code is redundant. In all the "optimisations", it has been left in.

    If we're voting, I guess my preferred solution is in post #4

    Regards, TMS

  16. #16
    Registered User
    Join Date
    06-18-2014
    Location
    Netherlands
    MS-Off Ver
    2014
    Posts
    23

    Re: Make a macro more efficient/faster

    Thanks alot for all your answers! I forgot about that screenupdating!
    I used your 2nd version TMS, works perfect, went from 5 min waiting to like 10 seconds lol, still quite long but ahwell, better then a few minutes.


    Now I'm wondering, how do I transpose it so I can hide/unhide columns? Just like my initial post.

  17. #17
    Registered User
    Join Date
    06-18-2014
    Location
    Netherlands
    MS-Off Ver
    2014
    Posts
    23

    Re: Make a macro more efficient/faster

    Oh and ragulduy, I used your method with the hidden columns, works faster then the macro! I can change this easy method to the horizontal way, thanks!

  18. #18
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,514

    Re: Make a macro more efficient/faster

    You're welcome. Thanks for the rep.

+ 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] Need Help Making Macro Faster/More Efficient
    By rbac in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-09-2013, 10:52 AM
  2. [SOLVED] Sumproduct - formula more efficient/faster?
    By Gti182 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 06-17-2013, 07:45 AM
  3. [SOLVED] More efficient (faster) sub-String method?
    By Bytor47 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-12-2013, 08:40 AM
  4. [SOLVED] More efficient and faster way to delete Rows?
    By nironto in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 06-07-2013, 05:01 AM
  5. [SOLVED] Edit code to be faster, more efficient.
    By rocksan in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-15-2012, 02:45 AM

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