Sub MrE1223268()
'https://www.mrexcel.com/board/threads/macro-with-multiple-headers-for-multiple-pages.1223268/
Dim lngPrint As Long
Dim rngLast As Range
Dim PDFsheets As String
Dim ws As Worksheet
Const cstrShData As String = "Individual Result GT"
Const cblnAlterSheetNumbers As Boolean = True
PDFsheets = ""
lngPrint = 1
Sheets(cstrShData).Copy After:=Sheets(Sheets.Count)
Set ws = ActiveSheet
PDFsheets = PDFsheets & ActiveSheet.Name & ","
With ws
With .UsedRange
.Value = .Value
End With
Set rngLast = .Cells(258, "F").End(xlUp).Offset(1, 0)
.Rows(rngLast.Row & ":" & Rows.Count).Delete
Application.PrintCommunication = False
With .PageSetup
.PrintTitleRows = "$1:$7"
.PrintTitleColumns = ""
End With
Application.PrintCommunication = True
.PageSetup.PrintArea = "$A$1:$T$" & .Cells(258, "F").End(xlUp).Row
Application.PrintCommunication = False
amkkhanPageSetup ws, cstrShData, lngPrint
Application.PrintCommunication = True
Set rngLast = Nothing
End With
lngPrint = lngPrint + ExecuteExcel4Macro("Get.document(50)")
Set ws = Nothing
Sheets(cstrShData).Copy After:=Sheets(Sheets.Count)
Set ws = ActiveSheet
PDFsheets = PDFsheets & ActiveSheet.Name & ","
With ws
With .UsedRange
.Value = .Value
End With
Set rngLast = .Cells(331, "B").End(xlUp).Offset(1, 0)
.Rows(rngLast.Row & ":316").Delete
.Rows("1:257").Delete
.Rows("56:257").Delete
.PageSetup.PrintArea = ""
.HPageBreaks(2).DragOff Direction:=xlDown, RegionIndex:=1
Application.PrintCommunication = False
With .PageSetup
.PrintTitleRows = "$1:$9"
.PrintTitleColumns = ""
End With
Application.PrintCommunication = True
.PageSetup.PrintArea = "$A$1:$T$" & .Cells(258, "H").End(xlUp).Row
Application.PrintCommunication = False
amkkhanPageSetup ws, cstrShData, lngPrint
Application.PrintCommunication = True
Set rngLast = Nothing
End With
lngPrint = lngPrint + ExecuteExcel4Macro("Get.document(50)")
Set ws = Nothing
Sheets(cstrShData).Copy After:=Sheets(Sheets.Count)
Set ws = ActiveSheet
PDFsheets = PDFsheets & ActiveSheet.Name & ","
With ws
With .UsedRange
.Value = .Value
End With
.Rows("1:331").Delete
.PageSetup.PrintArea = ""
.PageSetup.PrintArea = "$A$1:$T$" & .Cells(258, "H").End(xlUp).Row
Application.PrintCommunication = False
amkkhanPageSetup ws, cstrShData, lngPrint
Application.PrintCommunication = True
Set rngLast = Nothing
End With
Set ws = Nothing
Worksheets(Split(Left(PDFsheets, Len(PDFsheets) - 1), ",")).Select
If cblnAlterSheetNumbers Then Application.Dialogs(xlDialogPageSetup).Show
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:="C:\result\Result-" & Left(ThisWorkbook.Name, InStr(ThisWorkbook.Name, ".") - 1) & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
With Application
.DisplayAlerts = False
ActiveWindow.SelectedSheets.Delete
.DisplayAlerts = True
.ScreenUpdating = True
End With
MsgBox "Created 'C:\result\Result-" & Left(ThisWorkbook.Name, InStr(ThisWorkbook.Name, ".") - 1) & ".pdf'."
End Sub
Sub amkkhanPageSetup(ws As Worksheet, _
cstrShData As String, _
lngPrint As Long)
With ws.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftHeader = cstrShData
.CenterHeader = " "
.RightHeader = "&P"
.LeftFooter = "Class Incharge : ________________________________"
.CenterFooter = "Controller of Exam : ________________________________"
.RightFooter = "Principal : ________________________________"
.LeftMargin = Application.InchesToPoints(0.236220472440945)
.RightMargin = Application.InchesToPoints(0.236220472440945)
.TopMargin = Application.InchesToPoints(0.236220472440945)
.BottomMargin = Application.InchesToPoints(0.62992125984252)
.HeaderMargin = Application.InchesToPoints(0.31496062992126)
.FooterMargin = Application.InchesToPoints(0.31496062992126)
.PrintQuality = 600
.CenterHorizontally = True
.CenterVertically = True
.Orientation = xlLandscape
.Draft = False
.PaperSize = xlPaperA4
.FirstPageNumber = lngPrint
.Zoom = 41
End With
End Sub