Inserting Page Numbers in Excel Cell

kimberly090

Board Regular
Joined
May 22, 2014
Messages
99
Hi, I would like to insert a page number inside my excel cell.

I don't to use the Header & Footer due to it will only appear on the top or bottom.

I wish to add the page numbers in the middle of the excel worksheet.

I have do some researching and this seen like only can be done using vba code.

I have my code as below:

Code:
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

But this will only return me result on 1, 2, 3 after I type on =pagenumbers()

Is there possible to let it return me with page of pages?

Thank you.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

Forum statistics

Threads
1,216,084
Messages
6,128,721
Members
449,465
Latest member
TAKLAM

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top