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