Export only one sheet as PDF

damnfabio

New Member
Joined
May 30, 2022
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Hello

Thanks for the help in advance!

I wanted to have a button on a landing page of a workbook to print a specific sheet named "leather letter" on the excel file.

So basically I would like that when someone clicks that button a prompt shows up to select the location to print only that sheet in pdf.

Can someone help me with this? I've searched everywhere and couldn't find a good answer!

Thank you 🙌
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
connect your button to this code:

VBA Code:
Public Sub SheetToPDF()

   Dim DestFolder As String
  
   DestFolder = GetFolder
   If GetFolder <> "" Then
      Worksheets("leather letter").ExportAsFixedFormat Type:=xlTypePDF, Filename:=DestFolder & "\Leather Letter.PDF"
   End If

End Sub

Function GetFolder() As String

   Dim fldr As FileDialog
  
   Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
   With fldr
       .Title = "Select a Folder"
       .AllowMultiSelect = False
       '.InitialFileName = strPath
       If .Show <> -1 Then GoTo NextCode
       GetFolder = .SelectedItems(1)
   End With
  
   Set fldr = Nothing

End Function
 
Upvote 0
connect your button to this code:

VBA Code:
Public Sub SheetToPDF()

   Dim DestFolder As String
 
   DestFolder = GetFolder
   If GetFolder <> "" Then
      Worksheets("leather letter").ExportAsFixedFormat Type:=xlTypePDF, Filename:=DestFolder & "\Leather Letter.PDF"
   End If

End Sub

Function GetFolder() As String

   Dim fldr As FileDialog
 
   Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
   With fldr
       .Title = "Select a Folder"
       .AllowMultiSelect = False
       '.InitialFileName = strPath
       If .Show <> -1 Then GoTo NextCode
       GetFolder = .SelectedItems(1)
   End With
 
   Set fldr = Nothing

End Function
Hi!!

Thank you so much for you reply! I really appreciate your help.

I've tried the code above but it's giving me the error in attach.

Any suggestion? :)
 

Attachments

  • Screenshot_2.jpg
    Screenshot_2.jpg
    104.3 KB · Views: 11
  • Screenshot_1.jpg
    Screenshot_1.jpg
    118.3 KB · Views: 11
Upvote 0
I'm very sorry, replace that function with this one:

VBA Code:
Function GetFolder(Optional Title As String = "Select folder", Optional InitialFolder As String) As String

   With Application.FileDialog(msoFileDialogFolderPicker)
      .AllowMultiSelect = False
      .Title = IIf(Title = "", "Select a Folder", Title)
      .ButtonName = "Select Folder"
      .InitialView = msoFileDialogViewDetails
      .InitialFileName = IIf(InitialFolder = "", ActiveWorkbook.Path, InitialFolder)
    
      If .Show <> -1 Then
         GetFolder = ""
      Else
         GetFolder = .SelectedItems(1)
      End If
   End With

End Function
 
Upvote 0
Solution
I'm very sorry, replace that function with this one:

VBA Code:
Function GetFolder(Optional Title As String = "Select folder", Optional InitialFolder As String) As String

   With Application.FileDialog(msoFileDialogFolderPicker)
      .AllowMultiSelect = False
      .Title = IIf(Title = "", "Select a Folder", Title)
      .ButtonName = "Select Folder"
      .InitialView = msoFileDialogViewDetails
      .InitialFileName = IIf(InitialFolder = "", ActiveWorkbook.Path, InitialFolder)
   
      If .Show <> -1 Then
         GetFolder = ""
      Else
         GetFolder = .SelectedItems(1)
      End If
   End With

End Function
Hi Jeff!

This worked perfectly!

Just wondering if there's a way of changing the way the prompt shows up to save the file.

It shows up to select the folder. Is it possible to change the code to present the typical "save as" window?

Thank you
 
Upvote 0
Do you want to allow the user to specify the folder location and the actual file name?

I think there is a way to do what you want, so that it will look like a Save As dialog. But it will be completely different code than what I've provided, starting from scratch.
 
Upvote 0
Do you want to allow the user to specify the folder location and the actual file name?

I think there is a way to do what you want, so that it will look like a Save As dialog. But it will be completely different code than what I've provided, starting from scratch.
ahh,

I understand.

Apologies, for not explaining correctly from the beginning.
That would be in fact the best solution.
Are you able to help me with this? 🙏
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,971
Members
452,371
Latest member
Frana

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