VBA Code to Make Separate PDF for each Worksheet in the File

hrayani

Well-known Member
Joined
Jul 23, 2010
Messages
1,500
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I am using the below code to make a seperate excel file for each worksheet in the same folder.


Code:
[COLOR=#333333]Sub Splitbook()[/COLOR]

[COLOR=#333333]Dim xPath As String[/COLOR]
[COLOR=#333333]xPath = Application.ActiveWorkbook.Path[/COLOR]
[COLOR=#333333]Application.ScreenUpdating = False[/COLOR]
[COLOR=#333333]Application.DisplayAlerts = False[/COLOR]
[COLOR=#333333]For Each xWs In ThisWorkbook.Sheets[/COLOR]
[COLOR=#333333]xWs.Copy[/COLOR]
[COLOR=#333333]Application.ActiveWorkbook.SaveAs Filename:=xPath & "" & xWs.Name & ".xlsx"[/COLOR]
[COLOR=#333333]Application.ActiveWorkbook.Close False[/COLOR]
[COLOR=#333333]Next[/COLOR]
[COLOR=#333333]Application.DisplayAlerts = True[/COLOR]
[COLOR=#333333]Application.ScreenUpdating = True[/COLOR]
[COLOR=#333333]End Sub[/COLOR]


Now i would want the code to make a pdf for each worksheet instead of excel file.

Any help would be appreciated..

Regards,

Humayun
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Try,

Code:
Sub Splitbook_pdf()
    Dim xPath   As String
    Dim xWs     As Worksheet
    xPath = Application.ActiveWorkbook.Path & "\"
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
        For Each xWs In ThisWorkbook.Sheets
            xWs.Copy
            xWs.ExportAsFixedFormat Type:=xlTypePDF, Filename:=xPath & "" & xWs.Name & ".pdf"
            Application.ActiveWorkbook.Close False
        Next
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks Vds,

The code you provided is working perfect :)

Thanks,

Humayun
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,582
Members
449,039
Latest member
Arbind kumar

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