Button with macro to save as pdf

Maci3j

Board Regular
Joined
Apr 4, 2013
Messages
85
Office Version
  1. 365
Dear reader,

I have a file: Rapport, with two tabs: 1) Results and 2) Chart.

I would like to create a button on the results-tab, so when I click on it the two tabs (results and chart) wil be saved as pdf-file.

Can you please help me?
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Maci3j,

Try asigning this to your button...

Code:
Sub PDF_Click()
On Error Resume Next
If Not Application.Version > 11.9 Then
Resp = MsgBox("Need to use Excel 2007 or later for this!!", vbOKOnly, "Sorry!")
On Error GoTo 0
Exit Sub
End If
'Set the path to PDF folder  -- edit to suit
MyPath = ThisWorkbook.Path & "\PDFs"  'Same path as the xl file in a subfolder PDFs
MyName = ThisWorkbook.Name
MyName = Left(MyName, InStr(1, MyName, ".") - 1) & ".pdf"

'*** edit out this message if not required
Resp = MsgBox("You will find the PDF file within the folder named 'PDFs'" & Chr(13) & MyPath, _
vbOKOnly, "Creating PDF File")
'******************
Application.ScreenUpdating = False
MkDir MyPath 'folder will be created if not exist otherwise error ignored
Kill MyPath & "\" & MyName 'file will be deleted and overwritten if exist otherwise error ignored
    ThisWorkbook.Sheets(Array("Results", "Chart")).Select
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        MyPath & "\" & MyName, Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
        Sheets("Data").Select
On Error GoTo 0
End Sub

Hope that helps.
 
Upvote 0
Maci3j,

Try asigning this to your button...

Code:
Sub PDF_Click()
On Error Resume Next
If Not Application.Version > 11.9 Then
Resp = MsgBox("Need to use Excel 2007 or later for this!!", vbOKOnly, "Sorry!")
On Error GoTo 0
Exit Sub
End If
'Set the path to PDF folder  -- edit to suit
MyPath = ThisWorkbook.Path & "\PDFs"  'Same path as the xl file in a subfolder PDFs
MyName = ThisWorkbook.Name
MyName = Left(MyName, InStr(1, MyName, ".") - 1) & ".pdf"

'*** edit out this message if not required
Resp = MsgBox("You will find the PDF file within the folder named 'PDFs'" & Chr(13) & MyPath, _
vbOKOnly, "Creating PDF File")
'******************
Application.ScreenUpdating = False
MkDir MyPath 'folder will be created if not exist otherwise error ignored
Kill MyPath & "\" & MyName 'file will be deleted and overwritten if exist otherwise error ignored
    ThisWorkbook.Sheets(Array("Results", "Chart")).Select
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        MyPath & "\" & MyName, Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
        Sheets("Data").Select
On Error GoTo 0
End Sub

Hope that helps.

Thank you VERY MUCH, i'll try it.
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,521
Members
449,088
Latest member
RandomExceller01

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