VBA Error: “Compile error: Expected End Sub”

You can’t nest a function inside a procedure. You need to move it above:

Function GetFullNamePDF() As String
    GetFullNameCSV = Replace(ThisWorkbook.FullName, ".xlsm", ".pdf")
    'This should be
    GetFullNamePDF = Replace(ThisWorkbook.FullName, ".xlsm", ".pdf")
End Function

Sub PrintPDF()

     'Remove the quotes from GetFullNamePDF
     ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        GetFullNamePDF(), Quality:=xlQualityStandard, IncludeDocProperties _
        :=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

End Sub

Leave a Comment