Excluding sheets when saving to separate PDF files, VBA coding

FreAnd

New Member
Joined
Feb 8, 2022
Messages
10
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hi all,

I am a bit new to VBA coding but have managed to create a macro that lets me save multiple worksheets to separate pdf's.
But, I don't want to include two specific sheets, for ex, sheet TEMPLATE and SOURCE DATA how can I exclude these two and print the rest?

This is my code:


Sub LoopSheetsSaveAsPDF()



'Create variables

Dim ws As Worksheet



'Loop through all worksheets and save as individual PDF in same folder

'as the Excel file

For Each ws In ActiveWorkbook.Worksheets





ws.ExportAsFixedFormat Type:=xlTypePDF, _

Filename:=ThisWorkbook.Path & "/" & ws.Name & ".pdf"



Next



End Sub




I would be very grateful for any assistance



My best regards from a cold Sweden,
Fredrik
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
could you not include it in the if statement, something like

Excel Formula:
For Each ws In ActiveWorkbook.Worksheets

     If ws.name <> "Template" or ws.name <> "Source Data" then

          ws.ExportAsFixedFormat Type:=xlTypePDF, _

          Filename:=ThisWorkbook.Path & "/" & ws.Name & ".pdf"
     else
     end if

Next

End Sub
 
Upvote 0
Hi,

Thank you for the reply.

I tried the If statement like above but It will still not exclude the specified sheets.
 
Upvote 0
With this I can exclude the Source data sheet but still not the Template sheet,

Sub LoopSheetsSaveAsPDF()

'Create variables
Dim ws As Worksheet

'Loop through all worksheets and save as individual PDF in same folder
'as the Excel file
For Each ws In ActiveWorkbook.Worksheets

If Not ws.Name <> "Template" Or ws.Name <> "Source data" Then


ws.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=ThisWorkbook.Path & "/" & ws.Name & ".pdf"


Else
End If
Next

End Sub

Probably some newbie mistake by me, ;/
 
Upvote 0
the code i posted will look for specific name. If there is a leading space then it will not recognise the name.
try replacing it with
Excel Formula:
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
     If Not ws.Name like "Template" Or not ws.Name like "Source data" Then
          ws.ExportAsFixedFormat Type:=xlTypePDF, _
          Filename:=ThisWorkbook.Path & "/" & ws.Name & ".pdf"
     Else
     End If
Next
End Sub
 
Upvote 0
Solution
That works! Thank you very much for your assistance :)
 
Upvote 0
Welcome to the MrExcel Message Board!

For future reference.
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Saving all worksheets to seperate PDF files but excluding specific worksheets - OzGrid Free Excel/VBA Help Forum
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
That works! Thank you very much for your assistance :)
glad i could help. If this has solved your question then please mark as resolved by clicking the tick next to the solution
 
Upvote 0

Forum statistics

Threads
1,214,571
Messages
6,120,302
Members
448,954
Latest member
EmmeEnne1979

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