Create a folder and save a specific sheet in PDF format

sofas

Well-known Member
Joined
Sep 11, 2022
Messages
559
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
Hello how can I create a new folder and save a certain sheet inside it as PDF and name it with a specific cell value. Taking into account that I copy the data several times, each time I must create a new folder in case it does not already exist with the name of cell B5


VBA Code:
Private Sub CommandButton1_Click()

Dim ws As Worksheet
Dim strPath As String
Dim myFile As Variant
Dim strFile As String
On Error GoTo errHandler

Set ws = Sheet4


'enter name and select folder for file
' start in current workbook folder
strFile = ThisWorkbook.Path & "\" & strFile

myFile = Application.GetSaveAsFilename _
(InitialFileName:=strFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")

If myFile <> "False" Then
ws.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=myFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False

MsgBox "done"
End If

exitHandler:
Exit Sub
errHandler:
MsgBox "Error"
Resume exitHandler
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
So first you need to check if the subdirectory named in B5 of the activesheet(??) exists, and if "Not" then create it; then go on with the export code.
Try adding this block:
VBA Code:
Set ws = Sheet4
'ADDED BLOCK >>
strFile = ThisWorkbook.Path & "\" & Range("B5").Value    'Target Path
If Dir(strFile, vbDirectory) = "" Then
    MkDir strFile
    MsgBox "Created " & strFile
Else
    MsgBox "Directory " & strFile & " already there"
End If
'<<<END ADDED BLOCK
'Continue with your code:
myFile = Application.GetSaveAsFilename(InitialFileName:=strFile, _
 FileFilter:="PDF Files (*.pdf), *.pdf", _
 Title:="Select Folder and FileName to save")

If myFile <> False Then                       '!!!  Modified
'etc etc
Note the line marked !!! that need to be mofied as shown
The 2 MsgBox are optional, of course

Try...
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,173
Members
453,021
Latest member
Justyna P

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