Here is the code:
Rem Attribute VBA_ModuleType=VBAModule
Option VBASupport 1
Sub ExportEachSheet2PDF()
'Created by Yoda Learning Academy
'This VBA code will export each sheet of the workbook
'to a PDF file
'
Dim objWS As Worksheet
Dim strFileName As String
Dim strPath As String
Dim strCrntWS As String
strPath = ThisWorkbook.Path 'Assigned current file path
strCrntWS = ActiveSheet.Name 'Assigned current sheetname
Application.ScreenUpdating = False 'Disabled screen updating
For Each objWS In Worksheets
objWS.Select
strFileName = objWS.Name & ".pdf" 'Assigned worsksheet name as filename
'Following code will put '\' if it is not there
'-----------------------------------------------
If VBA.Right(strPath, 1) <> Application.PathSeparator Then
strPath = strPath & Application.PathSeparator
End If
'The following code will decide the export file type and then export it
'to the given path considering other required parameters.
'-----------------------------------------------------------------------
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=strPath & strFileName, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False 'This will prevent to open the file after publishing
Next objWS
Worksheets(strCrntWS).Select 'After exporting all the worksheets, it will return to active sheet
Application.ScreenUpdating = True 'Enabled screen updating
MsgBox "All sheets have been exported as PDF.", vbInformation + vbOKOnly, "Exported as PDF"
End Sub
I can’t save it as ODF, I only have ODS and OTS. I have saved it as XLSM.
The macro is supposed to create a PDF-file of each sheet in the excel-file.
I don’t know how to upload a sample file, I have put it here, I hope you can download it:
Regards
Staffan
[erAck: edited to format code as code, see This is the guide - How to use the Ask site? - #6 by erAck]