Best fit print area

  • I have a sheet with several rows which, during the normal use of the sheet, may get expanded height-wise, basically meaning the sheet may get longer when a use fill it out. I'm trying to figure out how to set up a macro to make the sheet print in a best-fit sort of manner, and that would run at print-time.


    To explain a little further, the sheet right now has a bunch of page breaks which I guess were automatic Excel breaks. So, if a user fills in very little information, then some of the pages will have just a few lines, and the rest blank.


    There was a discussion on this here , but I'm curious if there's a way to make a macro just insert a page break every 'x' number of pixels, rounding to the nearest full row, until it reaches the end of the sheet.


    Thanks.

  • OK, after inspecting the code from the other thread mentioned above, I've sort of fit it to my needs as follows.



    This code now seems to make a "best-fit" for length. If I want the width fixed, say, coumns A through I, how would I modify this code?

  • Re: Best fit print area


    After running the above code, I now can't move my righ-hand vertical page break beyond the right side of column H, but I need it at the right side of column I. Can anyone help?
    Thanks.

  • Re: Best fit print area


    OK, I've sorted out my latest issue (from my last post). I now have the following code. I wanted a confirm box before formatting the area. However, when I print, it just prints without popping up the message box. I don't know if this is a problem with my message box usage, or my BeforePrint usage. Can anyone tell me?
    Incidentally, the FitPages sub has been tested and is working fine.
    Thanks.


  • Re: Best fit print area


    Can anyone suggest why the BeforePrint method is not working for me? I've tried a simple Hello World msgBox, and even that does not work.

  • Re: Best fit print area


    This code needs to be in ThisWorkbook in VBE

    This code can be in a standard module.

    Barbara - aka The Cat Lady :cat:


    Please do not U2U (private message) me directly for questions that should be posted to the forum; any such U2Us, unless requested by me, will be deleted.

  • Re: Best fit print area


    I don't get a ThisWorkbook in VBA. I know what you're talking about because I've used the ThisOutlookSession project in Outlook VBA before, but in my project explorer in Excel VBA, I just see the current VBA project, which is just named after the file I'm in, and another one from the Auto-Save Add-In.


    How should I access ThisWorkbook?

  • Re: Best fit print area


    The message in your MsgBox isn't quite intuitive. It's really multiple outcomes.


    Format Costing Review before printing?


    Yes reformats and proceeds to printing.
    No skips reformatting and proceeds to printing.


    But Cancel cancels printing.


    Message might be more informative if:

    Code
    response = MsgBox("Format Costing Review before printing? (Yes or No)" & Chr(10) & Chr(10) & _
                        "Click Cancel to cancel printing.", vbYesNoCancel, "Printing check")

    Barbara - aka The Cat Lady :cat:


    Please do not U2U (private message) me directly for questions that should be posted to the forum; any such U2Us, unless requested by me, will be deleted.

  • Re: Best fit print area


    OK, thanks for the tip. I'll work that in. Also, ignore my last question. I was looking for something that looked like a seperate project. I found ThisWorkbook.
    Thanks again.

  • Re: Best fit print area


    Quote from slickity

    I don't get a ThisWorkbook in VBA. I know what you're talking about because I've used the ThisOutlookSession project in Outlook VBA before, but in my project explorer in Excel VBA, I just see the current VBA project, which is just named after the file I'm in, and another one from the Auto-Save Add-In.


    How should I access ThisWorkbook?

    In the VBE, do you see the Microsoft Excel Objects under the project name? Click on the + to expand the list of Excel Objects.

    Barbara - aka The Cat Lady :cat:


    Please do not U2U (private message) me directly for questions that should be posted to the forum; any such U2Us, unless requested by me, will be deleted.

  • Re: Best fit print area


    Quote from slickity

    OK, thanks for the tip. I'll work that in. Also, ignore my last question. I was looking for something that looked like a seperate project. I found ThisWorkbook.
    Thanks again.

    That's good but unfortunately the bell had already rung. :)

    Barbara - aka The Cat Lady :cat:


    Please do not U2U (private message) me directly for questions that should be posted to the forum; any such U2Us, unless requested by me, will be deleted.

  • Re: Best fit print area


    OK, I just spotted a problem with what I've done:


    I only want this macro FitPages to run when the sheet "Project Review" is being printed, which will only occur if a normal print command is run when that sheet is active, or if the entire workbook is being printed. How would I work this in?

  • Re: Best fit print area


    Quote from slickity

    OK, I just spotted a problem with what I've done:


    I only want this macro FitPages to run when the sheet "Project Review" is being printed, which will only occur if a normal print command is run when that sheet is active, or if the entire workbook is being printed. How would I work this in?

    Ok, not a big problem but have to figure out where to put the code that tests if Project Review is one of the selected worksheets.


    Obviously, if Project Review isn't one of the select worksheets to print, then what's the point of the MsgBox?


    So if the code tests before the MsgBox, then that MsgBox shouldn't display unless Project Review is selected. If not selected, then do you still want a MsgBox where the user can Cancel the printing?

    Barbara - aka The Cat Lady :cat:


    Please do not U2U (private message) me directly for questions that should be posted to the forum; any such U2Us, unless requested by me, will be deleted.

  • Re: Best fit print area


    Nope. Something I'm trying to figure out is, can you call a print command from within the BeforePrint method, or will this always cause an infinite loop (since that new print command will itself call the BeforePrint (BP), etc.)?


    The reason I ask is, I want some formatting changes to occur to the sheet "Projec Review" before printing, then be reversed after. There was another discussion about post-printing code here, but after thinking about the code I suggested, it seemed like it would make an infinite loop. The only way I can see of getting around this is if there's some more basic print command you can use in Excel that will ignore the BP method, so that you could call that from within BP.

  • Re: Best fit print area


    Also the BeforePrint is triggered by Print and by Print Preview.


    I could be wrong, but if Project Review worksheet is not the active worksheet, and is not selected along with other worksheets, then the BeforePrint code is run before the Print window is even displayed where the user can choose to print the Entire Workbook.


    So what I don't think can happen is that only one worksheet is selected and that worksheet is NOT Project Review. Then the BeforePrint code will not be run. So if the user selects to print the Entire Workbook, the Project Review worksheet will be printed without reformatting Project Review.

    Barbara - aka The Cat Lady :cat:


    Please do not U2U (private message) me directly for questions that should be posted to the forum; any such U2Us, unless requested by me, will be deleted.

  • Re: Best fit print area


    I think, but I could be wrong, that you will need to provide your own Print button and but all the code in there that you want/need.

    Barbara - aka The Cat Lady :cat:


    Please do not U2U (private message) me directly for questions that should be posted to the forum; any such U2Us, unless requested by me, will be deleted.

  • Re: Best fit print area


    Yeah, that's the same conclusion I've come to, and that's what I've done, with the print button being on the Project Review page. Now, the only other thing I'd like to do is if the entire workbook is being printed, that macro should run.

  • Re: Best fit print area


    The code for printing a single sheet versus the whole book is as follows (sure you knew this already).


    Code
    Sub printSingle()
    
    
        ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
    End Sub
    Sub printAll()
    
    
        ActiveWorkbook.PrintOut Copies:=1, Collate:=True
    End Sub


    So in light of there being two different commands, are there then two different versions of Before_Print for them?

  • Re: Best fit print area


    OK, I've realized this thread has gotten way off topic from where I started initially. The code I have now to do the conditional printing is as follows, and was provided here by user royUK.


    Following is a modified version of his code:



    This works fine for my needs, I'm just posting it so others can see.
    Now I want to return to my original question about formatting for printing.


    The following is the code I have, which is supposed to give a best-fit number of pages for the height of the document. However, it is unpredictable. Sometimes it will make good calls, like three pages or so for a document that realistically is around 3 pages long. Right now however, it attempts to cram about a 5-pager onto one page. Can anyone see a problem with this code?
    Some of the individual rows in my sheet grow in height as the user fills in info, but I don't think that explains the problem here.


  • Re: Best fit print area


    Nevermind. The problem with the formatting code was the line


    Code
    s = s + Rows(i).Width


    Should be


    Code
    s = s + Rows(i).Height


    I think that solves all my problems for now, at least related to the original question of this thread.


    Thanks everyone for your help.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!