VBA for exporting Excel worksheets to PDF

Devilfish2006k

Board Regular
Joined
Jun 7, 2006
Messages
80
Hi guys

I have an Excel workbook that is saved in a folder. What I would like to do is have a VBA routine that loops through all the worksheets that are called 'Payment 1' 'Payment 2' to 'Payment z', export to PDF and save the individual sheets/ PDFs in the same folder as the Excel workbooks as the worksheet tab names. There will be other worksheets that are not called 'Payment' but I want to ignore these

Many thanks
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Install ASAP Utilities. It adds a bunch of functions to Excel, one of them being saving each sheet as a separate file. PDF is one of the options for saving the files.
 
Upvote 0
.
Code:
Option Explicit


Sub ExportToPDFs()
Dim nm As String
Dim ws As Worksheet
Dim saveInFolder As String
    
    saveInFolder = ThisWorkbook.Path
    If Right(saveInFolder, 1) <> "\" Then saveInFolder = saveInFolder & "\"
    


    For Each ws In Worksheets
       
            If (ws.Name Like "Payment*") Then
                
                    ws.Select
                    nm = ws.Name
            
                    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
                    Filename:=saveInFolder & nm & ".pdf", _
                    Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                    IgnorePrintAreas:=False, OpenAfterPublish:=False
            End If
        
    Next ws
        
End Sub
 
Upvote 0
Thanks when I run this I get a Run-time error '1004' Document not saved. The document maybe open, or an error may have been encountered when saving.....
 
Upvote 0
Do you have data on the sheets named Payment ? It won't work if the sheet is blank.

Otherwise, the macro runs as expected here.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top