Save PDF as Activesheet name cell value

KyleG

Well-known Member
Joined
Jun 12, 2004
Messages
629
Office Version
  1. 365
Platform
  1. Windows
Hi,

Can someone help me change this recorded macro to save the pdf file name. Instead of test I want the file saved as the Worksheet Name and the value of B2.
So it might be DeptA 201917, or DeptB 201918 as examples.

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Data\test.pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
True

I want this to all happen from another worksheet called Entry and all the Dept pages are actually hidden. With this formula I would need to unhide them and make them the activesheet, but could each sheet be saved without activating them?

Cheers for your help. I haven't done any VBA for about 5 years and I ant find anything in my old notes to assist.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hi Kyle,

What's the logic for the code to know how to PDF which tabs? Is it all hidden tabs, do the relevant tab names have "dept" in them, something else?

Robert
 
Upvote 0
So we have DeptA, DeptB, and DeptC as the hidden department sheet names, the code would apply to all of them. It does a number of other things to other pages and the final step I want it to do is take the 3 Dept worksheets and export these as PDFs as DeptA 201917, DeptB 201917, and DeptC 201917, next week cell B2 on each of these worksheets will have updated to 201918 (along with other data) and so when the macro is run it will save a new file DeptA 201918, deptB 201918 and DeptC 201918.

is that what I needed to explain better?
 
Upvote 0
Yes, that's great. Try this:

Code:
Option Explicit
Sub Macro2()

    Dim wsMySheet As Worksheet
    Dim varMySheet As Variant
    
    Application.ScreenUpdating = False
    
    For Each varMySheet In Array("DeptA", "DeptB", "DeptC") 'Sheets to have PDF's created from. Change to suit.
        Set wsMySheet = Sheets(CStr(varMySheet))
        wsMySheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\Data\" & wsMySheet.Range("B2").Value & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
    Next varMySheet
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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