VBA Export each ws with condition to separate PDF

AJS1984

New Member
Joined
Aug 17, 2021
Messages
3
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
  2. Web
Trying to loop through each sheet and export to PDF if Cell A1 = 1, using ws name in PDF name

I got this far and was able to export all conditional to one PDF but can't get it to separate files, Currently script doesn't do anything and im stumped.

VBA Code:
Public Sub Export_Sheets_To_PDF()

    Dim saveInFolder As String
    Dim replaceSelected As Boolean
    Dim wsName As Variant
    Dim ws As Worksheet

    saveInFolder = "C:\Users\Documents\01-ReportFiles\"
    'Save to onedrive
    'saveInFolder = ThisWorkbook.Path
    If Right(saveInFolder, 1) <> "\" Then saveInFolder = saveInFolder & "\"
    
    With ThisWorkbook
        replaceSelected = True
         For Each ws In Worksheets
            If Range("A1").Value = 1 Then
                .Worksheets(ws).ExportAsFixedFormat Type:=xlTypePDF, Filename:=saveInFolder & .Worksheets("Master_00").Range("K2").Text & "_" & ws & "_" & Format(Date, "YYYY-MM-DD") & ".pdf", _
                    Quality:=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
            End If
        Next   
    End With
End Sub

Any pointers where i've messed this up?

Thanks
AJ
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Replace the loop with:
VBA Code:
         For Each ws In .Worksheets
            If ws.Range("A1").Value = 1 Then
                ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=saveInFolder & .Worksheets("Master_00").Range("K2").Text & "_" & ws.Name & "_" & Format(Date, "YYYY-MM-DD") & ".pdf", _
                    Quality:=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
            End If
        Next
 
Upvote 0
Replace the loop with:
VBA Code:
         For Each ws In .Worksheets
            If ws.Range("A1").Value = 1 Then
                ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=saveInFolder & .Worksheets("Master_00").Range("K2").Text & "_" & ws.Name & "_" & Format(Date, "YYYY-MM-DD") & ".pdf", _
                    Quality:=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
            End If
        Next
Thanks for taking a look @John_w

When i run it it throws a

2021-08-18 00_14_23-Microsoft Visual Basic for Applications - 00-Master_V001.xlsm [running] - ...png


2021-08-18 00_15_12-Microsoft Visual Basic for Applications - 00-Master_V001.xlsm [break] - [M...png


I had a similar error and removed the ws. on the IF which cleared the Type mismatch but then threw

2021-08-18 00_15_43-Microsoft Visual Basic for Applications - 00-Master_V001.xlsm [running] - ...png

2021-08-18 00_15_53-Microsoft Visual Basic for Applications - 00-Master_V001.xlsm [break] - [M...png


This is where my current vba knowledge lacks.

Any ideas would be greatly appreciated ?
 
Upvote 0
When i run it it throws a

2021-08-18 00_14_23-Microsoft Visual Basic for Applications - 00-Master_V001.xlsm [running] - ...png


2021-08-18 00_15_12-Microsoft Visual Basic for Applications - 00-Master_V001.xlsm [break] - [M...png

What is the value of A1 on the sheet which fails? Does it contain an error such as #N/A, #DIV/0!, #NUM!, etc.?

Check for the error like this:
VBA Code:
    With ThisWorkbook
         For Each ws In .Worksheets
            If Not IsError(ws.Range("A1").Value) Then
                If ws.Range("A1").Value = 1 Then
                    ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=saveInFolder & .Worksheets("Master_00").Range("K2").Text & "_" & ws.Name & "_" & Format(Date, "YYYY-MM-DD") & ".pdf", _
                        Quality:=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
                End If
            End If
        Next
    End With
 
Upvote 0
Solution
What is the value of A1 on the sheet which fails? Does it contain an error such as #N/A, #DIV/0!, #NUM!, etc.?

Check for the error like this:
VBA Code:
    With ThisWorkbook
         For Each ws In .Worksheets
            If Not IsError(ws.Range("A1").Value) Then
                If ws.Range("A1").Value = 1 Then
                    ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=saveInFolder & .Worksheets("Master_00").Range("K2").Text & "_" & ws.Name & "_" & Format(Date, "YYYY-MM-DD") & ".pdf", _
                        Quality:=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
                End If
            End If
        Next
    End With

Thanks John_w, that did the trick. Didn't think I had any errors but I had some that were letters and therefore couldn't parse the comparison.
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,387
Members
449,080
Latest member
Armadillos

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