+ Reply to Thread
Results 1 to 5 of 5

Number of pages in the workbook

Hybrid View

  1. #1
    Registered User
    Join Date
    03-11-2010
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2003 + 2007
    Posts
    3

    Question Number of pages in the workbook

    I have a workbook with 3 sheets. Each of these sheets can contain up to 5 pages. The formatting is different on each sheet.
    My customer wants to see "Page x of y" written in a certain cell (not in the header - that would be too easy ) and y = number of pages in the workbook!
    I found some scripts that can determine the number of pages in a sheet but none that can determine the number of pages in the whole workbook.
    Anybody got any hint how to solve this problem?

    Thanks, Harry

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,880

    Re: Number of pages in the workbook

    Quote Originally Posted by HarryX View Post
    I found some scripts that can determine the number of pages in a sheet but none that can determine the number of pages in the whole workbook.
    I could do this from scratch but it will be easier to start with what you already have. If you post the code that determines pages in a sheet I'll show you how to iterate through all the sheets and add up pages in the whole workbook.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    03-11-2010
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2003 + 2007
    Posts
    3

    Re: Number of pages in the workbook

    This worked best so far:

      Public Function PageNumber( _
                    Optional ByRef rng As Excel.Range) As Variant
            Dim pbHorizontal As HPageBreak
            Dim pbVertical As VPageBreak
            Dim nHorizontalPageBreaks As Long
            Dim nVerticalPageBreaks As Long
            Dim nPageNumber As Long
    
            On Error GoTo ErrHandler
            Application.Volatile
            If rng Is Nothing Then _
                Set rng = Application.Caller
            With rng
                If .Parent.PageSetup.Order = xlDownThenOver Then
                    nHorizontalPageBreaks = .Parent.HPageBreaks.Count + 1
                    nVerticalPageBreaks = 1
                Else
                    nHorizontalPageBreaks = 1
                    nVerticalPageBreaks = .Parent.VPageBreaks.Count + 1
                End If
                nPageNumber = 1
                For Each pbHorizontal In .Parent.HPageBreaks
                    If pbHorizontal.Location.Row > .Row Then Exit For
                    nPageNumber = nPageNumber + nVerticalPageBreaks
                Next pbHorizontal
                For Each pbVertical In .Parent.VPageBreaks
                    If pbVertical.Location.Column > .Column Then Exit For
                    nPageNumber = nPageNumber + nHorizontalPageBreaks
                Next pbVertical
            End With
            PageNumber = nPageNumber
    ResumeHere:
            Exit Function
    ErrHandler:
            'Could use much more error handling...!
            PageNumber = CVErr(xlErrRef)
            Resume ResumeHere
        End Function

    I found it here

  4. #4
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile Re: Number of pages in the workbook

    Good evening HarryX
    Quote Originally Posted by HarryX View Post
    I found some scripts that can determine the number of pages in a sheet but none that can determine the number of pages in the whole workbook.
    John Walkenbach has a solution to this here on his old pages.

    HTH

    DominicB
    Please familiarise yourself with the rules before posting. You can find them here.

  5. #5
    Registered User
    Join Date
    03-11-2010
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2003 + 2007
    Posts
    3

    Re: Number of pages in the workbook

    Thanks dominicb, looks like a good point to start from!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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