Хорошо, я хочу поместить в правом нижнем колонтитуле номер страницы для отчета Excel.
Для этого я использую макрос VBA для генерации страниц, и информация копируется из из листа сведений заголовка к копии листа шаблона, которая затем используется в качестве фактической страницы для отчета.
Проблема в том, что вводная страница печатается с исходным значением, как и ожидалось, но при переходе на следующую страницу она случайным образом увеличивается на 8, так что становится «страница 9 из х». Как я могу остановить его от этого случайного прыжка?
Страницы отчета
Sub ReportPages()
Dim areas As Integer
Dim pageNumberTotal As Integer
areas = 1
' Unhides the Template sheet so it is ready to be used.
Worksheets("Template").Visible = True
' Add new pages based on the header details sheet.
Sheets("Header Details").Select
' Select cell A14 as the basis to fill out the template with data.
Range("A14").Select
Do While IsEmpty(ActiveCell) = False
ActiveCell.Offset(1, 0).Select
areas = areas + 1
Loop
pageNumberTotal = areas + 5
' Matches the amount of areas tested that have been specified in the Header Details sheet
Do While areas > 1
For i = 1 To Worksheets.Count
If Worksheets(i).Name = areas - 1 Then
exists = True
End If
Next i
If exists = True Then
areas = areas - 1
exists = False
Else
' Decrement by 1 and copy the relevant data to the template.
areas = areas - 1
Sheets("Template").Select
Sheets("Template").Copy After:=Worksheets("Template")
Sheets("Template (2)").Select
Sheets("Template (2)").Name = areas
Range("I6").Select
ActiveCell = areas
' Call the WetDry function and then protect the sheet.
Call WetDry
End If
Loop
' Closes the template sheet when it is done.
Worksheets("Template").Visible = False
'If ActiveSheet.Name = 1 Then
'Dim pageNumberSetting As String
'Dim pageNumber As Integer
'pageNumber = 1
'Sheets("Front Page").Select
' Sets the font type and size of the page number and page total in the bottom right hand corner of the page.
'pageNumberSetting = "&B&9Page " & pageNumber & " of " & pageNumberTotal & " &K00+000." & Chr(10) & "" & Chr(10) & "" & Chr(10) & ""
'With ActiveSheet.PageSetup
'.RightFooter = pageNumberSetting
'End With
'pageNumber = pageNumber + 1
'ActiveSheet.Next.Activate
'End If
' Calls the next function and passes the value of the page number setting.
Call FrontBackPages
End Sub
Передняя и задняя страницы
Sub FrontBackPages()
' Sets the preliminary features for the start of the report.
' Declarations of variables.
If ActiveSheet.Name = 1 Then
Dim pageNumberSetting As String
Dim pageNumber As Integer
pageNumber = 1
Sheets("Front Page").Select
' Debug message - please ignore.
' MsgBox " The Name of the active sheet is " & ActiveSheet.Name
' Sets the font type and size of the page number and page total in the bottom right hand corner of the page.
pageNumberSetting = "&B&9Page " & pageNumber & " of " & pageNumberTotal & " &K00+000." & Chr(10) & "" & Chr(10) & "" & Chr(10) & ""
With ActiveSheet.PageSetup
.RightFooter = pageNumberSetting
End With
pageNumber = pageNumber + 1
ActiveSheet.Next.Activate
' Selects the "Appx Summary" sheet and propegates it with information from other parts of the workbook,
' generates a page number for this part of the report.
Do While ActiveSheet.Name <> "Appx Summary"
pageNumberParameter = "&B&9Page " & pageNumber & " of " & pageNumberTotal & " &K00+000."
' If the active sheet condition is met then the "Slip Resistance Testing" sheet is selected and is
' given a page number that will be placed in the lower right hand corner of the page.
If ActiveSheet.Name = "Slip Resistance Testing" Then
With ActiveSheet.PageSetup
.FirstPage.RightFooter.Text = pageNumberParameter
End With
pageNumber = pageNumber + 1
pageNumberParameter = "&B&9Page " & pageNumber & " of " & pageNumberTotal & " &K00+000."
End If
' The "Template" sheet is selected and the page number is decremented by 1.
If ActiveSheet.Name = "Template" Then
pageNumber = pageNumber - 1
End If
' The active sheet is selected and in the right - hand footer is given a page number.
' After this the next sheet is activated.
With ActiveSheet.PageSetup
.RightFooter = pageNumberParameter
End With
pageNumber = pageNumber + 1
ActiveSheet.Next.Activate
Loop
' The page number is then added to the page and also gives the total page number as well.
' This will place the page number in the bottom right hand corner of the page..
pageNumberParameter = "&B&9Page " & pageNumber & " of " & pageNumberTotal & " &K00+000."
With ActiveSheet.PageSetup
.FirstPage.RightFooter.Text = pageNumberParameter
End With
End If
' Selectes the "Header Details" sheet and the prompts the user that the pages have been successfully added.
Sheets("Header Details").Select
MsgBox "Pages Added!"
End Sub