Is there a formula or VBA code in Excel that can indicate in a cell how many pages of the so many pages in a specific sheet consists of? Don't give me any comment to put it in de footer of header please. That i now how to do it. Bruno from Belgium.
Bruno SegersOP
@Reply 5 years ago
This code does show the correct number of pages, but when printing, it stops its function on the following pages. Is there anyone who can help me further? I have attached the print preview and file.
Option Explicit
Sub Pagenumber()
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 = "REV 1 - Strona " & xNumPage & " - " & Application.ExecuteExcel4Macro("GET.DOCUMENT(50)")
End Sub
Hi Bruno. It's been a while since I've done anything like this with Excel VBA (Access is my forte). However, if memory serves you can calculate the number of printed pages in a Worksheet with:
Worksheets(i).PageSetup.Pages.Count
Also, please review the Forum Rules. I don't allow the posting of external links.
Sorry, only students may add comments.
Click here for more
information on how you can set up an account.
If you are a Visitor, go ahead and post your reply as a
new comment, and we'll move it here for you
once it's approved. Be sure to use the same name and email address.
This thread is now CLOSED. If you wish to comment, start a NEW discussion in
Excel Forum.