+ Reply to Thread
Results 1 to 2 of 2

Find page number of cell - speed up code

Hybrid View

  1. #1
    Registered User
    Join Date
    10-20-2014
    Location
    The Netherlands
    MS-Off Ver
    2007
    Posts
    29

    Question Find page number of cell - speed up code

    Dear all,

    I found this great (well, somewhat) piece of code, but it's very slow. Does anyone have an idea how to speed it up?

    I found it at: http://www.mcgimpsey.com/excel/udfs/pagenumber.html

    Here's the code:

       '******************************************************
       'Purpose: Display the page number a cell will be printed on
       'Inputs:  target The cell of interest
       '         nStart The starting page number
       'Returns: The page number of the target cell. Returns
       '         #Ref! if there's an error
    
       '******************************************************
          Public Function PageNumber( _
                    Optional ByRef target As Excel.Range, _
                    Optional ByVal nStart As Long = 1&) As Variant
            Dim pbHorizontal As HPageBreak
            Dim pbVertical As VPageBreak
            Dim nHorizontalPageBreaks As Long
            Dim nPageNumber As Long
            Dim nVerticalPageBreaks As Long
            Dim nRow As Long
            Dim nCol As Long
            
            On Error GoTo ErrHandler
            Application.Volatile
            If target Is Nothing Then _
                Set target = Application.Caller
            With target
                nRow = .Row
                nCol = .Column
                With .Parent
                    If .PageSetup.Order = xlDownThenOver Then
                        nHorizontalPageBreaks = .HPageBreaks.Count + 1&
                        nVerticalPageBreaks = 1&
                    Else
                        nHorizontalPageBreaks = 1&
                        nVerticalPageBreaks = .VPageBreaks.Count + 1&
                    End If
                    nPageNumber = nStart
                    For Each pbHorizontal In .HPageBreaks
                        If pbHorizontal.Location.Row > nRow Then Exit For
                        nPageNumber = nPageNumber + nVerticalPageBreaks
                    Next pbHorizontal
                    For Each pbVertical In .VPageBreaks
                        If pbVertical.Location.Column > nCol Then Exit For
                        nPageNumber = nPageNumber + nHorizontalPageBreaks
                    Next pbVertical
                End With
            End With
            PageNumber = nPageNumber
        ResumeHere:
            Exit Function
        ErrHandler:
            'Could use much more error handling...!
            PageNumber = CVErr(xlErrRef)
            Resume ResumeHere
        End Function

  2. #2
    Registered User
    Join Date
    10-20-2014
    Location
    The Netherlands
    MS-Off Ver
    2007
    Posts
    29

    Re: Find page number of cell - speed up code

    For your information, I found out that the line:

    Application.Volatile
    ... is absolutely killer for the speed of Excel all together. This is something I found out only recently and therefore haven't written a workaround, but I know where it's going wrong.

+ 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. VBA code to find the row number based on data via input box and the highlight a cell
    By Aditya Sabat in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-21-2014, 05:47 PM
  2. Replies: 1
    Last Post: 01-28-2013, 02:14 AM
  3. How to find phone number in another page ?
    By Niko73 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-06-2009, 08:10 AM
  4. Replies: 0
    Last Post: 11-22-2005, 10:50 AM
  5. [SOLVED] Find the Page Number
    By Graham in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-14-2005, 06:06 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