Number of pages displayed in cell

Jimmypop

Well-known Member
Joined
Sep 12, 2013
Messages
753
Office Version
  1. 365
Platform
  1. Windows
Good day all

Some assistance required...

I need the number of pages for workbook (2 sheets)... hereafter is some info regarding the sheet/s

1. Sheet2 will always be 1 page (the last page)
2. Sheet1 starts of on 1 page and goes to a maximum of 8 pages.
3. Sheet1 in cell K2 should display the following Page 1 of 2 or Page 1 of 5 etc. depending on the number of pages on Sheet1 plus 1 page from Sheet2. (Number of pages on Sheet1 is determined by hiding and unhiding rows).
4. Sheet1 is in page break view.

So in short... after report is finished, K2 on Sheet1 should show the total number of pages of the workbook depending on the amount on Sheet1, e.g. Sheet1 has 4 pages so K2 should show Page 1 of 5 (4 pages from Sheet1 and 1 page from Sheet2) OR Sheet1 has 8 pages so K2 should show Page 1 of 9 (8 pages from Sheet1 and 1 page from Sheet2)...

Found this code below but it only shows the total of Sheet1 including hidden rows (Shows Page 1 of 8 all the time)

VBA 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
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
if you have no vertical page breaks you could use this function

VBA Code:
Function Pages()
    Application.Volatile
    Pages = Application.Caller.Parent.HPageBreaks.Count + 1
End Function


Book5
ABCDEFGHIJKL
11
22Page 1 of 3
33
44
55
66
77
88
99
1010
1111
1212
1313
1414
1515
1616
1717
1818
1919
2020
2121
2222
2323
2424
2525
2626
2727
2828
2929
3030
3131
3232
3333
3434
3535
3636
3737
3838
3939
4040
4141
4242
4343
4444
4545
4646
4747
4848
4949
5050
5151
5252
5353
5454
5555
5656
5757
5858
5959
6060
61
Sheet1
Cell Formulas
RangeFormula
A1:A60A1=SEQUENCE(60)
K2K2="Page 1 of " & pages()+1
Dynamic array formulas.
 
Upvote 0
if you have no vertical page breaks you could use this function

VBA Code:
Function Pages()
    Application.Volatile
    Pages = Application.Caller.Parent.HPageBreaks.Count + 1
End Function


Book5
ABCDEFGHIJKL
11
22Page 1 of 3
33
44
55
66
77
88
99
1010
1111
1212
1313
1414
1515
1616
1717
1818
1919
2020
2121
2222
2323
2424
2525
2626
2727
2828
2929
3030
3131
3232
3333
3434
3535
3636
3737
3838
3939
4040
4141
4242
4343
4444
4545
4646
4747
4848
4949
5050
5151
5252
5353
5454
5555
5656
5757
5858
5959
6060
61
Sheet1
Cell Formulas
RangeFormula
A1:A60A1=SEQUENCE(60)
K2K2="Page 1 of " & pages()+1
Dynamic array formulas.
Hi @JGordon11

Unfortunately I do have vertical page breaks as well... Code still gives pages as 8 but only 4 are visible and the rest are hidden...
 
Upvote 0
Has anyone else perhaps have any other ideas that could work?... Thanks
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

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