+ Reply to Thread
Results 1 to 22 of 22

Dynamic print range

  1. #1
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Dynamic print range

    I have spent the last two days trying to get a dynamic print range to work but im not having any luck. If anyone could take a look (to save me from pulling out my hair!) I would much appreciate it.
    Im trying to create a dynamic pint range for the Sheet 'Search'. The contents of which varies by the search criteria entered in the top search fields.
    Eventually I want to create a macro to print, and a macro to make a PDF of the search results.
    Many thanks in advance for taking the time.
    James
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: Dynamic print range (please help!)

    Hi,

    To alter the print range using VBA, use the following code (altered for your specific circumstances):
    Please Login or Register  to view this content.
    To make the print range dynamic, create a variable that will find the last row that has a non-blank value in it, and reference this to be the last row that is defined within the print area:
    Please Login or Register  to view this content.
    Hope this helps

  3. #3
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Re: Dynamic print range (please help!)

    Thank you very much for your response. The VB Dynamic print range code worked a treat!
    Is there a way I can set the top rows (with the document title and column headers) to be at the top of every page? B10-J12
    Also is there a way in the code to set it to always print out in Black and White (I have tried selecting this function in Page Setup - Sheet, but it seems to revert back and become unselected every now and then), resulting in the cell background colors being printed out.
    Many Thanks,
    James

  4. #4
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: Dynamic print range (please help!)

    Hi,

    The black and white property is printer-specific, and can not be changed in VBA, as the printer preferences will always override. So short of you running a macro to change the colours to black and white prior to printing, and then changing them back to colour again after printing, this one isn't going to be possible sorry.

    To get the headers at the top of every page, I would suggest running several print macros, rather than one single piece of code. So it would go something like this (pseudocode-ish):
    Please Login or Register  to view this content.
    Hope this helps

  5. #5
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Re: Dynamic print range (please help!)

    Thanks, that makes sense. Im just not sure what the code would be (especially to including the first code you gave me that finds the last row of data). The rows to repeat at the top are B10:J12. The first page is B13-J35 (plus the top 3 rows header rows). Every page after that is 23 rows of data (plus top 3 rows) so B36-J58, B59-J81 etc etc (the doc ends at 702 which is 30 pages).
    Thanks,
    James

  6. #6
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: Dynamic print range

    Sorry, wrong thread

    Will get back to this thread in a moment

  7. #7
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: Dynamic print range

    Hi,

    Try this:
    Please Login or Register  to view this content.
    This finds the last used row first, then for each sheet finds the last row for that page. It then prints out that page, then hides those rows. Then if it has printed all of the used rows, all rows are unhidden and printing is stopped (so that you don't print out any unused rows).

    Hope this is what you're looking for

  8. #8
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Re: Dynamic print range

    Is there a way I can test it without having to print it out (can I just view the PDF preview or something).

  9. #9
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: Dynamic print range

    Unfortunately, if you want the headings at the top of every page, the way this has to be done is to print x number of 1-page documents, rather than 1 x-page document.

    What I'm getting at is, yes you can export to PDF instead of printing while you are just testing, but you won't get a single PDF file, you will get multiple single-page PDF files.

    Instead of
    Please Login or Register  to view this content.
    use
    Please Login or Register  to view this content.
    Let me know how this goes

  10. #10
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Re: Dynamic print range

    I replaced it with that code (just changing the directory to be mac friendly), but I get run time error 1004 on that line.

  11. #11
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: Dynamic print range

    Hmmm, OK I guess I should have looked at the fact that you are using Excel Mac.

    Here's a file with that line of code that works on my computer (using Excel 2010 for Windows), can you please tell me if it works for you? It should save 2 PDFs ("Page01.pdf" and "Page02.pdf") into the directory where you save this attachment.

    If it doesn't, it is more than likely an incompatibility with the Mac version of Excel.

    Thanks
    Attached Files Attached Files

  12. #12
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Re: Dynamic print range

    Didnt work, its coming up with the same 1004 error.

  13. #13
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: Dynamic print range

    OK after some looking around, I'm hoping this may fix your issue...change
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    on that line of code only.

    Let me know how you go...

  14. #14
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: Dynamic print range

    Or if this doesn't work, I believe that there is a "Save As PDF" option built in to Excel 2011 Mac in the printing dialog.

    So perhaps try setting this as the default printer in Excel and then executing the .PrintOut code from a few posts above.

    Failing this, I really can't help with this part of your problem sorry, I've never used Excel Mac and have never debugged any issues in it either.

  15. #15
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: Dynamic print range

    Actually, going back to the basics here, but have you triple checked that your file path is correct? As I believe an invalid path would produce a 1004 error...

  16. #16
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Re: Dynamic print range

    Sorry its taken me some time to get back to you. I couldn't get the PDF print to work, so I just had to keep printing on paper. A couple of trees later I determined it doesn't quite seem to be working. It keeps coming up with errors on this line:

    endPage = (23 * i) + 12

    And it tends to print alot of blank pages with just the header and footers.

  17. #17
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: Dynamic print range

    Could you please upload an updated version of your workbook with all of this code in it so that I can debug it please? This all seemed to work fine in the mock-up workbook that I made before suggesting it, so I would like to see why it isn't working in your workbook.

    Thanks

  18. #18
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Dynamic print range

    Quote Originally Posted by Nitefox View Post
    I replaced it with that code (just changing the directory to be mac friendly), but I get run time error 1004 on that line.
    I realise I've come late to this thread and perhaps stating the obvious, but are you allowing for the fact that the path separator on a Mac is the : rather than the \ ?

    There are several differences in syntax and object behaviour between Excel for Mac/Windows but the path separator is the first one to check.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  19. #19
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: Dynamic print range

    Thanks Richard, I knew the Windows path syntax wasn't the same as the Mac path syntax, but having never used a Mac (n00b, I know! ) I wasn't sure what the difference is.

    I still think the reason for the 1004 error is an invalid path, but this doesn't explain why
    Please Login or Register  to view this content.
    would be throwing an error...which is why I am keen to see how the code has been implemented...

  20. #20
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Re: Dynamic print range

    Well after some tinkering I think its now working for me. I removed the top three rows (the column title and document title) from the print area in the code, and added them to the repeat print rows in Page Setup instead, which helped. I also manually moved around some of the print lines.
    I used the same code along with Ron de Bruin's PDF email code, to email pdfs.
    Thank you for all your help, and for sticking with me on this issue.

  21. #21
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: Dynamic print range

    Glad to hear you got it working, and I am happy to have helped.

    Please don't forget to mark this thread as solved and please click on the * next to my post(s) to say thanks

  22. #22
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Re: Dynamic print range

    Sorry im new to this forum. How do I mark it as solved?

+ 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 ‘Set Print Area’ Code for Dynamic Print Range based on Conditions
    By dosbirn in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-16-2013, 12:13 PM
  2. [SOLVED] Automatically Print Once Dynamic Print Range is Identified
    By ccowman in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-27-2013, 10:34 AM
  3. Print Titles with Dynamic Print Range
    By Daeghen in forum Excel General
    Replies: 0
    Last Post: 08-01-2011, 12:11 PM
  4. dynamic print range
    By Tom44556 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-05-2007, 06:57 PM
  5. [SOLVED] Dynamic Print Range Help
    By waxwing in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-21-2005, 12: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