Save Multiple Sheets In Workbook As PDF Based on Cell Condition And Name As Cell Value

YourHouseKey

New Member
Joined
Mar 13, 2021
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
Hello,

Looking for assistance with saving multiple sheets in a workbook as individual PDFs in a defined directory where the file name is based on a cell value. However, I only want to save sheets when a cell within that sheet meets a certain criteria.

EXAMPLE: If cell B116 = Y, Then save sheet as PDF with a file name based cell B120. If cell B116 <> Y, Then move on to the next sheet.

I'd like this to sweep through all sheets in my workbook and only export sheets where cell B116 = Y.

Can anybody help with some VBA code to provide a solution?

Thank you!
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Welcome to MrExcel forums.

Try this macro, changing the saveInFolder as required.
VBA Code:
Public Sub Create_PDFs()

    Dim saveInFolder As String
    Dim ws As Worksheet
    
    saveInFolder = "C:\path\to\folder\"     'CHANGE THIS
    If Right(saveInFolder, 1) <> "\" Then saveInFolder = saveInFolder & "\"
    
    With ActiveWorkbook
        For Each ws In .Worksheets
            If ws.Range("B116").Value = "Y" Then
                ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=saveInFolder & ws.Range("B120").Text & ".pdf", _
                    Quality:=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
            End If
        Next
    End With
    
End Sub
 
Upvote 0
Solution
Welcome to MrExcel forums.

Try this macro, changing the saveInFolder as required.
VBA Code:
Public Sub Create_PDFs()

    Dim saveInFolder As String
    Dim ws As Worksheet
   
    saveInFolder = "C:\path\to\folder\"     'CHANGE THIS
    If Right(saveInFolder, 1) <> "\" Then saveInFolder = saveInFolder & "\"
   
    With ActiveWorkbook
        For Each ws In .Worksheets
            If ws.Range("B116").Value = "Y" Then
                ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=saveInFolder & ws.Range("B120").Text & ".pdf", _
                    Quality:=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
            End If
        Next
    End With
   
End Sub
Hi John_w,

Thank you for providing. This worked perfectly in my workbook. Ran into a couple of runtime errors, but a few changes to my workbook resolved the issues.

I appreciate the assistance.

Thanks again!
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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