Save file as PDF excluding one sheet

skf786

Board Regular
Joined
Sep 26, 2010
Messages
105
hi,

i want a macro to save the file as PDF on desktop excluding the sheet named 'Home' There are four other sheets in the file named 1,2,3,4.

File name should be from cell A1 on Home sheet plus dd/mm/yyyy.

Thank you

KF
 

Some videos you may like

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,977
Office Version
  1. 365
Platform
  1. Windows
is the macro permitted to close the workbook immediately after creating the pdf ?
- one method would be to save the workbook, delete the unwanted sheet, create pdf, close the workbook without saving

Otherwise a new workbook needs to be created - which you may prefer.
- if so is that workbook to be retained or deleted ?
- should original workbook be left open or closed ?
 

skf786

Board Regular
Joined
Sep 26, 2010
Messages
105
No, the workbook has to remain open and the saved PDF has to open after saving as well.

Would prefer a new workbook to be created which may be deleted.
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,977
Office Version
  1. 365
Platform
  1. Windows
I forgot about a simpler method
- assumes the code is in the workbook containing the 5 sheets
- dd/mm/yyyy contains illegal characters hence dd-mm-yyyy
- amend fPath C:\Test\Folder\SubFolder

Try this
VBA Code:
Sub CreatePDF()
    Dim fName As String, fPath As String
    fPath = "C:\Test\Folder\SubFolder"
    fName = Sheets("Home").Range("A1").Value & " " & Format(Date, "dd-mm-yyyy")
    fPath = fPath & "\" & fName & ".pdf"
    ThisWorkbook.Sheets(Array("1", "2", "3", "4")).Select
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=fPath, _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, OpenAfterPublish:=False
    ThisWorkbook.Sheets("Home").Select
End Sub
 

skf786

Board Regular
Joined
Sep 26, 2010
Messages
105

ADVERTISEMENT

Thank you, this works fine. Is it possible to give a standard fPath so that it works on desktop of any user?

Thanks again

KF
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,977
Office Version
  1. 365
Platform
  1. Windows
try this

VBA Code:
fPath = CreateObject("WScript.Shell").SpecialFolders("Desktop")
MsgBox fPath
 

Watch MrExcel Video

Forum statistics

Threads
1,123,158
Messages
5,600,054
Members
414,357
Latest member
Gemma_R

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
Top