Public Sub Save_Sheet_As_PDF_With_Picture_Footer_On_Last_Page()
Dim pictureFile As String
Dim PDFoutputFile As String
Dim currentView As XlWindowView
Dim rowsPerPage As Long
Dim lastDataRow As Long
Dim lastPageLastRow As Long
Dim picShape As Shape
Dim footerCell As Range
pictureFile = CreateObject("WScript.Shell").SpecialFolders("MyDocuments") & "\My picture.png"
PDFoutputFile = ThisWorkbook.Path & "\Sheet with last page footer.pdf"
Application.ScreenUpdating = False
With ActiveWindow
currentView = .View
.View = xlPageBreakPreview
End With
With ActiveSheet
.ResetAllPageBreaks
If .HPageBreaks.Count > 0 Then
rowsPerPage = .HPageBreaks(1).Location.Row - 1
Else
rowsPerPage = 50
End If
lastDataRow = .UsedRange.Rows.Count + .UsedRange.Rows(1).Row - 1
lastPageLastRow = (.HPageBreaks.Count + 1) * rowsPerPage
Set picShape = .Shapes.AddPicture(pictureFile, False, True, 0, 0, -1, -1)
Set footerCell = .Cells(lastPageLastRow + 1, 1)
If footerCell.Top - 1 - picShape.Height < .Cells(lastDataRow + 1, 1).Top Then
.HPageBreaks.Add Before:=.Cells(lastPageLastRow + 1, 1).EntireRow
Set footerCell = .Cells(lastPageLastRow + rowsPerPage + 1, 1)
End If
picShape.Top = footerCell.Top - 1 - picShape.Height
End With
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFoutputFile, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=True, OpenAfterPublish:=True
picShape.Delete
ActiveWindow.View = currentView
Application.ScreenUpdating = False
End Sub