Vba - pdf a non active sheet

Viperuk

New Member
Joined
Mar 14, 2016
Messages
19
Hi all

I have a wb with a selection of sheets.

What I am trying to do is write code so I can pdf a specific sheet from a front page.

My idea is you select the first sheet and then press the button for the sheet you wish to pdf. I don't want the pdf button on every sheet.

I can get vba to pdf the active sheet, but I'm struggling to pdf a non active sheet from the active sheet.

Any advice will be appreciated.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Code:
Sub ExportToPDFs()

Dim ws As Worksheet

[COLOR=#006400][I]'a loop[/I][/COLOR]
For Each ws In Worksheets
    ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\Test\" & ws.Name & ".pdf"
Next ws
[COLOR=#006400][I]    
'single sheet[/I][/COLOR]
    Sheets("SheetName").ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\Test\" & "MyPDFname" & ".pdf"
[COLOR=#006400] 'or[/COLOR]
    Set ws = Sheets("SheetName")
    ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\Test\" & "MyPDFname" & ".pdf"
End Sub
 
Last edited:
Upvote 0
Thank you yongle

This is working, the problem I'm. Having is now with the file save name which are referenced to the same range in each sheet.

How can I specify the sheet required for the file name.

I will try and post the code
 
Upvote 0

Forum statistics

Threads
1,214,621
Messages
6,120,563
Members
448,972
Latest member
Shantanu2024

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