Save certain worksheets as PDFs in a fluid location

palaeontology

Active Member
Joined
May 12, 2017
Messages
444
Office Version
  1. 2016
Platform
  1. Windows
I currently use the following code to print all worksheets named with a 5-digit name (eg: 34716)

Code:
For Each ws In ThisWorkbook.Worksheets    If ws.Name Like "#####" Then
        ws.Range("P22:U22").Font.Color = vbWhite
        ws.Range("P22:U22").Interior.Color = vbWhite
        ws.PageSetup.Orientation = xlLandscape
        ws.PrintOut From:=1, To:=1
    End If
Next ws

I no longer want to print these sheets to paper. I would like to now save each worksheet that is named with a 5-digit code as a PDF to a location such as ...

G:\Maths Dept\STUDENT RESULTS\2019\PDF Profile Copies\CJAMI\12MathA\CHAPMAN, Ellee

however, the last 3 components of that address are fluid, and folders with those names may or may not already exist when the code is run.

In other words, there is definitely a folder at ...
G:\Maths Dept\STUDENT RESULTS\2019\PDF Profile Copies .... waiting to receive PDFs, but the "CJAMI\12MathA\CHAPMAN, Ellee" section of the address is dependent on the contents of cells S5, E2 and S3 (in that order) on each of the worksheets that are to be printed (the ones that are named with a 5-digit code).

If, for example, the "CJAMI" folder doesn't already exist inside the location "G:\Maths Dept\STUDENT RESULTS\2019\PDF Profile Copies" then it would need to be created.

and if the "12MathA" folder doesn't already exist inside the "CJAMI" folder, then it would need to be created

etc.

I know that to save as a PDF, the code would have to begin with something like ...

Code:
Sheets("Sheet1").Range("A1:H20").ExportAsFixedFormat Type:=xlTypePDF, _    Filename:="C:\Users\marks\Documents\Saved PDF.pdf"

however I really am struggling to figure how to set it up to save in a location with those three fluid components at the end (the ones that are reliant on whatever is in cells S5, E2 and S3 of the sheet being saved in PDF format.

Is this at all possible ?

 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Try this

Change data in red by your information.

Code:
Sub Save_worksheets_PDFs()
    Dim ws As Worksheet, wPath As String, r1 As String, r2 As String, r3 As String
    
    wPath = "G:\Maths Dept\STUDENT RESULTS\2019\PDF Profile Copies\"
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name Like "#####" Then
            r1 = ws.Range("S5") & "\"
            r2 = r1 & ws.Range("E2") & "\"
            r3 = r2 & ws.Range("S3") & "\"
            If Dir(wPath & r1, vbDirectory) = "" Then MkDir wPath & r1
            If Dir(wPath & r2, vbDirectory) = "" Then MkDir wPath & r2
            If Dir(wPath & r3, vbDirectory) = "" Then MkDir wPath & r3
            
            ws.Range("P22:U22").Font.Color = vbWhite
            ws.Range("P22:U22").Interior.Color = vbWhite
            ws.PageSetup.Orientation = xlLandscape
            ws.Range([B]"[COLOR=#ff0000]A1:U22[/COLOR]"[/B]).ExportAsFixedFormat xlTypePDF, wPath & r3 & ws.Name & ".pdf"
        End If
    Next ws
End Sub
 
Upvote 0
Strike that ... everything you wrote works perfectly. I was doing something wrong at my end.

Thankyou again so much for doing that.

It is very much appreciated.

Very kind regards,

Chris
 
Upvote 0
Strike that ... everything you wrote works perfectly. I was doing something wrong at my end.

Thankyou again so much for doing that.

It is very much appreciated.

Very kind regards,

Chris

I'm glad to help you. I appreciate your kind comments.
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,513
Members
448,967
Latest member
screechyboy79

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