save selected worksheets from the workbook to individual PDFs

zhengzhi8806

New Member
Joined
Apr 16, 2018
Messages
2
Hi
I am trying to write a macro to save selected worksheets from the workbook to individual pdfs. but I only got the macro to work to print all the worksheets from the workbook including summary tab and notes tab. I only want to print the tabs that are employee statements. Each individual tab is named as employee name. How can i get the code to work only print the employee statements.





Code:
Option Explicit


Sub WorksheetLoop()


Dim wsA     As Worksheet
Dim wbA     As Workbook
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile  As Variant
Dim WS_Count As Integer


' Set WS_Count equal to the number of worksheets in the active workbook.
Set wbA = ActiveWorkbook
WS_Count = wbA.Worksheets.Count
strPath = wbA.Path


'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
    strPath = Application.DefaultFilePath
End If
strPath = strPath & ""


' Begin the loop.
For Each wsA In wbA.Worksheets
wsA.Activate
    'replace spaces and periods in sheet name
   strName = Replace(wsA.Name, " ", "")
   strName = Replace(strName, ".", "_")


    'create default name for savng file
    strFile = strName & ".pdf"
    myFile = strPath & strFile


    Debug.Print myFile


    'export to PDF if a folder was selected
If myFile <> "False" Then
        ActiveSheet.ExportAsFixedFormat _
                Type:=xlTypePDF, _
                Filename:=myFile, _
                Quality:=xlQualityStandard, _
                IncludeDocProperties:=True, _
                IgnorePrintAreas:=False, _
                OpenAfterPublish:=False
  End If


Next wsA
End sub
 
Last edited by a moderator:

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hi & welcome to MrExcel.
Maybe something like
Code:
' Begin the loop.
For Each WsA In wbA.Worksheets
   [COLOR=#ff0000]If WsA.Name <> "Summary" And WsA.Name <> "notes" Then[/COLOR]
      WsA.Activate
      'replace spaces and periods in sheet name
      strName = Replace(WsA.Name, " ", "")
      strName = Replace(strName, ".", "_")
      
      'create default name for savng file
      strFile = strName & ".pdf"
      MyFile = strPath & strFile
      
      Debug.Print MyFile
      
      'export to PDF if a folder was selected
      If MyFile <> "False" Then
         ActiveSheet.ExportAsFixedFormat _
            Type:=xlTypePDF, _
            FileName:=MyFile, _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=False
      End If
  [COLOR=#ff0000] End If[/COLOR]
Next WsA
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,246
Members
449,075
Latest member
staticfluids

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