Mirkaminer
New Member
- Joined
- Mar 22, 2022
- Messages
- 7
- Office Version
- 365
- Platform
- Windows
I have a workbook with several sheets. On every sheet i repet row 1 to 13 on each page. In cell C7 i want page number and total number of pages in the format of "1/3".
I have found this VBA code to do part of the problem
placed this part in a module
placed this part in the sheet object window
The part i have problems with is that i need iNumPage (the current page number, not the total number of pages)to update for every page that is going in to print.
Is there a way maybe piggyback riding of the page number function from header and footer, and using it in Private Sub Workbook_BeforePrint or is there another solution. The page number is only for cell C7 because of the repet of row 1-13 on top of each page in print out.
I have found this VBA code to do part of the problem
placed this part in a module
VBA Code:
Sub PageNumber(MyRange As String)
Dim iVPC As Integer
Dim iHPC As Integer
Dim iVPB As VPageBreak
Dim iHPB As HPageBreak
Dim iNumPage As Integer
iHPC = 1
iVPC = 1
If ActiveSheet.PageSetup.Order = xlDownThenOver Then
iHPC = ActiveSheet.HPageBreaks.Count + 1
Else
iVPC = ActiveSheet.VPageBreaks.Count + 1
End If
iNumPage = 1
For Each iVPB In ActiveSheet.VPageBreaks
If iVPB.Location.Column > ActiveCell.Column Then Exit For
iNumPage = iNumPage + iHPC
Next
For Each iHPB In ActiveSheet.HPageBreaks
If iHPB.Location.Row > ActiveCell.Row Then Exit For
iNumPage = iNumPage + iVPC
Next
MyRange = "'" & iNumPage & "/" & Application.ExecuteExcel4Macro("GET.DOCUMENT(50)")
End Sub
placed this part in the sheet object window
VBA Code:
Private Sub Worksheet_Activate()
Dim StrString As String
Call PageNumber(StrString)
Range("C7:C7").Value = StrString
End Sub
The part i have problems with is that i need iNumPage (the current page number, not the total number of pages)to update for every page that is going in to print.
Is there a way maybe piggyback riding of the page number function from header and footer, and using it in Private Sub Workbook_BeforePrint or is there another solution. The page number is only for cell C7 because of the repet of row 1-13 on top of each page in print out.