+ Reply to Thread
Results 1 to 11 of 11

vba to print the last 3 pages of a worksheet or less if document doesn't exceed 3 pages

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    03-17-2007
    Location
    Michigan
    MS-Off Ver
    Excel 2021 & 365
    Posts
    977

    vba to print the last 3 pages of a worksheet or less if document doesn't exceed 3 pages

    Hello, I found the following code online to print an excel worksheet starting with printing the last page first to the first initial page. So not sure this can be edited to do what I need or not but this is what I'm hoping to accomplish. I have a worksheet that has code that will filter automatically notes based on an account number. These notes are listed by date and can range from 1 page to 15 or more. Ideally, I would like a macro when activated to only print the last 3 pages of these notes or if if there aren't 3 pages to print then those remaining pages; i.e. 1 or 2 pages. So for further clarification if there would be 11 pages to print I would only want pages 11, 10, & 9 to print out in that order. If the filtered data would only result in 2 pages to print then it would just print page 2 and page 1. If filtered data has only 1 page to print it would just print the 1 page. I would also want this to print out in black and white so no conditional formatting colors should be seen when printed if possible. Any assistance would be greatly appreciated.

    Code I found online:
    Sub ReversePrint()
    Dim xPages As Long
    xPages = ExecuteExcel4Macro("GET.DOCUMENT(50)")
    For xIndex = xPages To 1 Step -1
        Application.ActiveSheet.PrintOut from:=xIndex, To:=xIndex
    Next
    'I found this for the black white portion but not sure if this is correct spot to put it?
    Worksheets("Note").PageSetup.BlackAndWhite = True
    End Sub
    Last edited by lilsnoop; 07-28-2023 at 08:10 AM.

  2. #2
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2505
    Posts
    2,792

    Re: vba to print the last 3 pages of a worksheet or less if document doesn't exceed 3 page

    This code will loop you through the last three sheets or two or one whatever the case may be if there are less than three.

    The debug.print statement will display the sheet name it's just so you can see the sheets that it's looping through and can be removed later.

    It uses the immediate window which can be opened by Control + G.

    I don't have much experience in printing with VBA perhaps someone else can help you if this isn't enough.

    Sub LoopThreeSheets()
    Dim c As Long, r As Long
    c = ThisWorkbook.Worksheets.Count
        For r = c To Application.Max(c - 2, 1) Step -1
        
            Debug.Print Worksheets(r).Name
            ' other code here
        
        Next r
    
    End Sub

  3. #3
    Valued Forum Contributor
    Join Date
    03-17-2007
    Location
    Michigan
    MS-Off Ver
    Excel 2021 & 365
    Posts
    977

    Re: vba to print the last 3 pages of a worksheet or less if document doesn't exceed 3 page

    Appreciate your help skywriter! I saved your code to a module and tried it but was not getting anything to print when I applied it to run.

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

    Re: vba to print the last 3 pages of a worksheet or less if document doesn't exceed 3 page

    If I read it right, you ask for pages of 1 sheet to print while skywriters code refers to sheets.

    But you have to supply more information.
    If you say the last three pages from a filtered area, how do you determine the pages? If you set a print range and do a print preview, usually the last page is not a full page.
    Do you consider this to be a page on its own? Or do you want the last page to be a full page of data like the other two.
    If so, you'll have to go by amount of rows per page and that varies, setups dependent.
    If your result is 50 rows per full page you want to print the last 150 rows for 3 pages. But this is only valid if all the rows are set to the same height, which we don't know if that is indeed so.
    As a kid I used to watch the wizard of OZ and wondered how someone could talk if they didn't have a brain. Then I got social media.

  5. #5
    Valued Forum Contributor
    Join Date
    03-17-2007
    Location
    Michigan
    MS-Off Ver
    Excel 2021 & 365
    Posts
    977

    Re: vba to print the last 3 pages of a worksheet or less if document doesn't exceed 3 page

    Quote Originally Posted by jolivanes View Post
    If I read it right, you ask for pages of 1 sheet to print while skywriters code refers to sheets.

    But you have to supply more information.
    If you say the last three pages from a filtered area, how do you determine the pages? If you set a print range and do a print preview, usually the last page is not a full page.
    Do you consider this to be a page on its own? Or do you want the last page to be a full page of data like the other two.
    If so, you'll have to go by amount of rows per page and that varies, setups dependent.
    If your result is 50 rows per full page you want to print the last 150 rows for 3 pages. But this is only valid if all the rows are set to the same height, which we don't know if that is indeed so.
    jolivanes-Thank you for your questions and review of my request. The data set is approximately 20,000 rows of data and when an account is selected on a different worksheet it activates a macro and will filter any/all notes affiliated to that account number on a different worksheet titled "Notes", which is the sheet I am trying to have a macro to print from. Currently to print the filtered data, I would have to go to the print preview and it could say I have 15 pages to print (affiliated to this particular account). I would then have to manually enter the page numbers of print 13 to 15 in order to only print the last three pages. It does not matter if the last page is a complete page of data or not. The notes can vary in length so some notes could take 6 rows or more while other notes could take 2 rows, etc, so unfortunately there is no set range in that regard. Some accounts when filtered may only have 2 pages worth of notes (based on the print preview) of data to print and for this type of scenario I would want those two pages to print out. I apologize I cannot provide a sample for test purposes. Hope that explains my situation better.

  6. #6
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2505
    Posts
    2,792

    Re: vba to print the last 3 pages of a worksheet or less if document doesn't exceed 3 page

    If I read it right, you ask for pages of 1 sheet to print while skywriters code refers to sheets.
    Got me on that one I was equating them in my head.

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

    Re: vba to print the last 3 pages of a worksheet or less if document doesn't exceed 3 page

    Happens to the best of us as you just proved!!!!

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

    Re: vba to print the last 3 pages of a worksheet or less if document doesn't exceed 3 page

    You don't need to quote. The same data is a couple posts up. Just clutter we don't want/need.
    If important, give the post number and maybe poster's name.

    Try
    Sub How_Far_Does_This_Get_You()
    Dim lr As Long, lc As Long, sh_notes As Worksheet
    Set sh_notes = Worksheets("Notes")
    lr = sh_notes.Cells.Find("*", , , , xlByRows, xlPrevious).Row
    lc = sh_notes.Cells.Find("*", , , , xlByColumns, xlPrevious).Column
    sh_notes.PageSetup.PrintArea = sh_notes.Cells(1, 1).Resize(lr, lc).Address
        With sh_notes
            If .PageSetup.Pages.Count < 4 Then
                .PrintOut
                    Else
                .PrintOut .PageSetup.Pages.Count - 2, .PageSetup.Pages.Count, 1
            End If
        End With
    End Sub

  9. #9
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2505
    Posts
    2,792

    Re: vba to print the last 3 pages of a worksheet or less if document doesn't exceed 3 page

    Make sure you review jolivanes code in post#8 if you haven't seen it.

    I found that same code you did and I did a little playing around.

    I can't say it's always the case but apparently you can count the horizontal page breaks and add 1 to them and know how many pages your printer is going to come up with if you use print preview.

    So I played around a little bit and came up with the below code and I will include the workbook.

    I can't say what other code would be needed because I don't currently have a printer so I print to pdf and on each loop it asks me for the file name, but it did print them out in reverse order.

    jolivanes code is probably what you want but I thought I would include this for fun.

    Sub PrintingTests()
    
    Dim numPages As Long, cntr As Long, lastPage2Print As Long
    
    numPages = Worksheets("Sheet1").HPageBreaks.Count + 1
        If numPages < 3 Then
            lastPage2Print = 1
        Else
            lastPage2Print = numPages - 2
        End If
    
        For cntr = numPages To lastPage2Print Step -1
            Worksheets("Sheet1").PrintOut From:=cntr, To:=cntr
        Next cntr
     
    End Sub
    Attached Files Attached Files
    Last edited by skywriter; 07-28-2023 at 12:43 PM.

  10. #10
    Valued Forum Contributor
    Join Date
    03-17-2007
    Location
    Michigan
    MS-Off Ver
    Excel 2021 & 365
    Posts
    977

    Re: vba to print the last 3 pages of a worksheet or less if document doesn't exceed 3 page

    Thank you both so much! I had to wait to test out both until I got to work. Both worked great. Skywriter, yours printed out in reverse too, which is much appreciated, but both printed the last 3 pages as hoped! Really appreciate both of your time spent and assistance with this problem!

  11. #11
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2505
    Posts
    2,792

    Re: vba to print the last 3 pages of a worksheet or less if document doesn't exceed 3 page

    My pleasure, thanks for the rep. points.

+ 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. Excel VBA to print specific pages from multiple sheets into one document
    By moxygrl88 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-17-2021, 04:11 PM
  2. Need vba code to print specific pages or all pages in a multipage UserForm
    By Pimp_mentality in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-24-2020, 08:13 PM
  3. [SOLVED] Macro to Print numbered pages of same document
    By Patish in forum Word Formatting & General
    Replies: 2
    Last Post: 03-19-2019, 03:41 PM
  4. Replies: 1
    Last Post: 03-24-2016, 05:28 PM
  5. Replies: 5
    Last Post: 12-15-2014, 05:45 PM
  6. Replies: 0
    Last Post: 01-21-2013, 07:56 PM
  7. [SOLVED] How can I get the 1st 2 rows on my document to print on all pages
    By j_a_barr in forum Excel General
    Replies: 1
    Last Post: 02-24-2006, 09: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