+ Reply to Thread
Results 1 to 15 of 15

Inefficient code - macro takes too long

  1. #1
    Registered User
    Join Date
    01-08-2014
    Location
    wigan
    MS-Off Ver
    2007
    Posts
    78

    Inefficient code - macro takes too long

    Hi, I have the following code which is supposed to export the pages in a workbook to pdf and attach it to an email. This works exactly as it should but takes far too long. it is also my first venture into macros and I'm pretty sure it is not as efficient as it should be - most of it has come from googling and copying other peoples code.

    Is there anything I can do to make the following quicker or more efficient:

    Please Login or Register  to view this content.
    [/CODE]

    in particular this bit I'm sure can be done better, or quicker:

    Please Login or Register  to view this content.
    thanks in advance.

  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,445

    Re: Inefficient code - macro takes too long

    I can't test your code, no workbook, no worksheets, no data, but the second half could be better written.

    Change:

    Please Login or Register  to view this content.

    To:

    Please Login or Register  to view this content.

    Not selecting sheets and ranges will make the code much quicker and means the final:

    Please Login or Register  to view this content.
    should be unnecessary.

    I don't know what the routine Create_PDF does, but you could probably just pass an array of worksheets to that instead of selecting the sheets and then calling the routine.

    It's not easy to provide a specific solution to a problem when most of the information required is "back at home".


    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 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,445

    Re: Inefficient code - macro takes too long

    May I ask what this construct does:

    Please Login or Register  to view this content.

    I've never seen anything like that and I don't understand how it works.

    Because "F1:shareholderfunds" is in quotes, that must be some sort of Named Range, but a Named Range cannot have a colon (":") in it.

    Confused.


    Regards, TMS

  4. #4
    Registered User
    Join Date
    01-08-2014
    Location
    wigan
    MS-Off Ver
    2007
    Posts
    78

    Re: Inefficient code - macro takes too long

    hi trevor, thanks.

    the create_pdf is a function to send the selection to a pdf. There are a few macro's using this function and all do it within a second or two. This one that you have helped me with takes around 20 seconds to do.

    For this:

    Please Login or Register  to view this content.
    The macro hides rows on each sheet before sending to pdf based on whether or not there is a 0 or 1 on each row in the K column on each sheet. The K column contains 1's or 0's based on IF functions for data on each row. Is this the best way to do this?

    the main problem here is that I have no idea what i'm doing - I am very new to VBA.

    I actually don't know what this even means:

    Please Login or Register  to view this content.
    I just know it works so I copied it for each page and changed the C to D, etc.

    Thank you very much for your help.

  5. #5
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Inefficient code - macro takes too long

    Please Login or Register  to view this content.
    That will refer to the range between cell F1 and the named range 'shareholderfunds', inclusive.
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  6. #6
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Inefficient code - macro takes too long

    Maybe this too.. not much saving in code space.. but i thought i would give it a try anyway..

    Please Login or Register  to view this content.

  7. #7
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,587

    Re: Inefficient code - macro takes too long

    Change this

    Please Login or Register  to view this content.
    as
    Please Login or Register  to view this content.
    similarly for others.

  8. #8
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Inefficient code - macro takes too long

    I am wondering if it would be more efficient to use Autofilter using "0" as the criteria and then just Hide all the visible rows..?

  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,445

    Re: Inefficient code - macro takes too long

    I am wondering if it would be more efficient to use Autofilter using "0" as the criteria and then just Hide all the visible rows..?

    And you would be left with what? Rows not selected in the filter would be hidden, and then youu hide the visible rows ...

  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,445

    Re: Inefficient code - macro takes too long

    @Olly:

    Quote Originally Posted by Olly View Post
    Please Login or Register  to view this content.
    That will refer to the range between cell F1 and the named range 'shareholderfunds', inclusive.

    Thank you. I did not know you could do that. I've always used either a range text string: Range("F1:F10"); a Named Range text string: Range("shareholderfunds"); or a string variable: Range(strShareholderfunds). And, looking at it, I wouldn't have thought that Excel/VBA would be prepared to evaluate that string into a static range and a Named Range part. I would, typically, define shareholderfunds as a Dynamic Named Range ... but then, I'm not sure how it is used here.

    Thanks again.

    Regards, TMS

  11. #11
    Registered User
    Join Date
    01-08-2014
    Location
    wigan
    MS-Off Ver
    2007
    Posts
    78

    Re: Inefficient code - macro takes too long

    hi,thanks for all the suggestions:

    what would this do, or what should it replace:

    Please Login or Register  to view this content.
    also, one other thing, which is the only thing stopping my document looking very professional, but the 6 page pdf that exports are different size pages, some are slightly wider than others and there appears to be no reason for this.

    Has anyone seen this before?

    Also, "shareholderfunds" is the name of a cell. It is the final line on a page of my document.

    thanks,
    dan.

  12. #12
    Registered User
    Join Date
    01-08-2014
    Location
    wigan
    MS-Off Ver
    2007
    Posts
    78

    Re: Inefficient code - macro takes too long

    the auto filter would be a good idea I think - what code would I use for that - I have the filter on K1

    thanks

  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,445

    Re: Inefficient code - macro takes too long

    the 6 page pdf that exports are different size pages, some are slightly wider than others and there appears to be no reason for this.

    Pretty difficult to comment without seeing some typical data

  14. #14
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Inefficient code - macro takes too long

    And you would be left with what? Rows not selected in the filter would be hidden, and then youu hide the visible rows ...
    When i read my post again.. I see i didn't word it too well..

    What i meant was AutoFilter the range using "0" as the criteria.. create a range with the visible cells.. turn off autofilter.. then hide the rows in the range that was just created..

    I am just thinking it might be faster than a loop..

    @dantray02.. its late here.. I will post what i mean with the autofilter tomorrow.. hopefully someone will chime in before that for you..
    Last edited by apo; 03-07-2014 at 08:44 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,445

    Re: Inefficient code - macro takes too long

    What i meant was AutoFilter the range using "0" as the criteria.. create a range with the visible cells.. turn off autofilter.. then hide the rows in the range that was just created..

    I am just thinking it might be faster than a loop..

    Sounds good

+ 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. Clunky Slow Code, Hides Empty Rows, Takes too long...
    By BoSonic in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-18-2013, 08:56 PM
  2. Replies: 0
    Last Post: 05-17-2013, 09: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] Counting how long a Macro takes to run
    By Diane Alsing in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-07-2005, 02:06 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