+ Reply to Thread
Results 1 to 4 of 4

Get Pages Ranges of PrintArea

Hybrid View

  1. #1
    Valued Forum Contributor Gatti's Avatar
    Join Date
    06-08-2015
    Location
    Brasil, São Paulo, Ribeirão Preto
    MS-Off Ver
    365
    Posts
    346

    Get Pages Ranges of PrintArea

    Hello!!!

    Is there any way to get ranges of pages in PrintArea?

    Using the default Worksheets("Sheet1").PageSetup.PrintArea only returns the full print area worksheet.

    I need to know where the "page breaks" and where the next page starts.

    Thanks!!
    If I have solved your question, please mark the thread as [SOLVED],
    And consider adding reputation

    ?Simplicity is the ultimate sophistication? (Leonardo Da Vinci)

    Regards,
    F?bio Gatti

    https://www.youtube.com/pulodogatti
    https://www.linkedin.com/in/fabiocgatti/
    https://instagram.com/pulodogatti

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,483

    Re: Get Pages Ranges of PrintArea

    Hi there,

    See if the following code gets you moving in the right direction:

    
    
    Option Explicit
    
    
    Sub LocatePageBreaks()
    
        Dim iNoOfPageBreaks As Integer
        Dim iLastRowOnSheet As Integer
        Dim iPageBreakNo    As Integer
        Dim vaPageRanges    As Variant
        Dim iRowNo_First    As Integer
        Dim iRowNo_Last     As Integer
        Dim sPagesList      As String
        Dim rPage           As Range
        Dim wks             As Worksheet
    
        Set wks = ActiveSheet
    
        With wks
            iLastRowOnSheet = .UsedRange.Rows(.UsedRange.Rows.Count).Row
        End With
    
        iNoOfPageBreaks = wks.HPageBreaks.Count
    
        iRowNo_First = 1
    
        If iNoOfPageBreaks > 0 Then
    
    '         The worksheet contains multiple pages
              ReDim vaPageRanges(1 To iNoOfPageBreaks)
    
              For iPageBreakNo = 1 To iNoOfPageBreaks
    
                  iRowNo_Last = wks.HPageBreaks(iPageBreakNo).Location.Row - 1
    
                  With wks
                      Set rPage = Range(.Rows(iRowNo_First), _
                                        .Rows(iRowNo_Last))
                  End With
    
                  Set vaPageRanges(iPageBreakNo) = rPage
    
                  iRowNo_First = iRowNo_Last + 1
    
              Next iPageBreakNo
    
        Else:
    
    '         The worksheet contains only a single page
              With wks
                  Set rPage = Range(.Rows(iRowNo_First), _
                                    .Rows(iLastRowOnSheet))
              End With
    
              ReDim vaPageRanges(1 To 1)
    
              Set vaPageRanges(1) = rPage
    
        End If
    
    '   Determine whether or not the worksheet contains used rows after the last PageBreak
        If iNoOfPageBreaks > 0 And _
           iLastRowOnSheet > iRowNo_Last Then
    
    '       If so, add the last page to the array of pages
            With wks
                Set rPage = Range(.Rows(iRowNo_First), _
                                  .Rows(iLastRowOnSheet))
            End With
    
            ReDim Preserve vaPageRanges(1 To UBound(vaPageRanges) + 1)
    
            Set vaPageRanges(UBound(vaPageRanges)) = rPage
    
        End If
    
    '   Display the list of page ranges
        sPagesList = vbNullString
    
        For iPageBreakNo = LBound(vaPageRanges) To UBound(vaPageRanges)
    
            sPagesList = sPagesList & _
                         vbLf & _
                         vbTab & vbTab & vaPageRanges(iPageBreakNo).Address
    
        Next iPageBreakNo
    
        MsgBox "The worksheet """ & wks.Name & """ contains the following " & _
               "page row range(s):" & _
                vbLf & _
                sPagesList
    
    End Sub

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

  3. #3
    Valued Forum Contributor Gatti's Avatar
    Join Date
    06-08-2015
    Location
    Brasil, São Paulo, Ribeirão Preto
    MS-Off Ver
    365
    Posts
    346

    Re: Get Pages Ranges of PrintArea

    Hey Greg! I'm sorry: took to long to reply... didn't enter the forum for almost a month

    It worked great! Thanks for your solution!

  4. #4
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,483

    Re: Get Pages Ranges of PrintArea

    Hi again,

    Many thanks for your feedback and also for the Reputation increase - much appreciated

    You're welcome - glad I was able to hep.

    Best regards,

    Greg M

+ 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. How to Save only pages with data (in specific ranges) as pdf's
    By dougjeffries in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-06-2015, 08:50 AM
  2. Replies: 1
    Last Post: 03-22-2013, 06:36 PM
  3. Need to autosum ranges of cells within multiple pages and then sum those as well.
    By nightbiscuit in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-22-2013, 05:48 PM
  4. Need to autosum ranges of cells within multiple pages and then sum those as well.
    By nightbiscuit in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-22-2013, 05:46 PM
  5. Put two chosen ranges on two different pages of one PDF document
    By bobby.bobby in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-14-2013, 09:01 AM
  6. Split total number of pages into ranges
    By judi0802 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 10-30-2012, 01:46 AM
  7. PrintArea
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-22-2005, 08:05 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