-
Mar 20th, 2018, 02:39 PM
#1
Thread Starter
New Member
Excel page counter in header
Hi, I don't know if im posting in the right place or even if this is possible to do.
I have a sheet which has a header in which in a specific place I would like it to count the pages when i print them out. This is the best code i found
Code:
Sub pagenumber()
'updateby Extendoffice 20160506
Dim xVPC As Integer
Dim xHPC As Integer
Dim xVPB As VPageBreak
Dim xHPB As HPageBreak
Dim xNumPage As Integer
xHPC = 1
xVPC = 1
If ActiveSheet.PageSetup.Order = xlDownThenOver Then
xHPC = ActiveSheet.HPageBreaks.Count + 1
Else
xVPC = ActiveSheet.VPageBreaks.Count + 1
End If
xNumPage = 1
For Each xVPB In ActiveSheet.VPageBreaks
If xVPB.Location.Column > ActiveCell.Column Then Exit For
xNumPage = xNumPage + xHPC
Next
For Each xHPB In ActiveSheet.HPageBreaks
If xHPB.Location.Row > ActiveCell.Row Then Exit For
xNumPage = xNumPage + xVPC
Next
ActiveCell = "Page " & xNumPage & " of " & Application.ExecuteExcel4Macro("GET.DOCUMENT(50)")
End Sub
However it doesn't work as I want it to, when I print it only prints out 1/9 pages, instead of 2/9 and so on
Thanks for any help
-
Mar 20th, 2018, 02:54 PM
#2
Re: Excel page counter in header
Just adding a header to the Excel workbook should do what you want.
https://support.office.com/en-us/art...5-c7ec07270f16
-
Mar 21st, 2018, 02:40 PM
#3
Thread Starter
New Member
Re: Excel page counter in header
The default excel header is placed in a specific place which I cant move, and this is a sheet in excel
-
Mar 21st, 2018, 03:09 PM
#4
Re: Excel page counter in header
can you post a sample workbook (zip first), with a sheet to add pagenumbers and a sheet with the desired result
this appeared to work correctly for a vertical only column of pages, you could do similar for multi columns
Code:
Dim xVPC As Integer
Dim xHPC As Integer
Dim xVPB As VPageBreak
Dim xHPB As HPageBreak
Dim xNumPage As Integer
xHPC = 1
xVPC = 1
col = 4 'column D change to suit
' page 1 as no pagebreak above
Cells(1, col) = "Page 1 of " & Application.ExecuteExcel4Macro("GET.DOCUMENT(50)")
If ActiveSheet.PageSetup.Order = xlDownThenOver Then
xHPC = ActiveSheet.HPageBreaks.Count + 1
Else
xVPC = ActiveSheet.VPageBreaks.Count + 1
End If
xNumPage = 1
For Each xVPB In ActiveSheet.VPageBreaks
If xVPB.Location.Column > ActiveCell.Column Then Exit For
xNumPage = xNumPage + xHPC
Next
Set sht = ActiveSheet
For Each xHPB In ActiveSheet.HPageBreaks
' If xHPB.Location.row > ActiveCell.row Then Exit For
xNumPage = xNumPage + xVPC
Cells(xHPB.Location.row, col) = "Page " & xNumPage & " of " & Application.ExecuteExcel4Macro("GET.DOCUMENT(50)")
Next
avoid using activecell (selection or active anything), when i ran your original code the "page of" was not even in the printed range
Last edited by westconn1; Mar 21st, 2018 at 03:36 PM.
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|