VBA - Create new folder for sheets saved to pdf

csimonds

Board Regular
Joined
Oct 2, 2011
Messages
73
Hello,

I have the below code that saves my required sheets as pdf to current folder. However I would like it to create a new folder in the current directory which will have a set text name.

Any assistance would be most appreciated.

Thank you

Code:
Sub SaveWorksheetsAsPDFs()    Dim sFile       As String
    Dim sPath       As String
    Dim wks         As Worksheet


    With ActiveWorkbook
        sPath = .Path & "\"
        For Each wks In .Worksheets
            If LCase(wks.Range("N2").Value) = "print" Then
                sFile = wks.Range("A6").Value & ".pdf"
                wks.ExportAsFixedFormat Type:=xlTypePDF, _
                                        Filename:=sPath & sFile, _
                                        Quality:=xlQualityStandard, _
                                        IncludeDocProperties:=False, _
                                        IgnorePrintAreas:=False, _
                                        OpenAfterPublish:=False
            End If
        Next wks
    End With
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
here you go

thank you for taking the time to present your code in a legible format

it makes it so much easier just to look at it on the webpage

Code:
Sub SaveWorksheetsAsPDFs()


    Dim sFile       As String
    Dim sPath       As String
    Dim wks         As Worksheet
 
    With ActiveWorkbook
        sPath = .Path & "\[COLOR=#ff0000]test123\[/COLOR]"
[COLOR=#ff0000]        MkDir sPath[/COLOR]
        For Each wks In .Worksheets
            If LCase(wks.Range("N2").Value) = "print" Then
                sFile = wks.Range("A6").Value & ".pdf"
                wks.ExportAsFixedFormat Type:=xlTypePDF, _
                                        Filename:=sPath & sFile, _
                                        Quality:=xlQualityStandard, _
                                        IncludeDocProperties:=False, _
                                        IgnorePrintAreas:=False, _
                                        OpenAfterPublish:=False
            End If
        Next wks
    End With
End Sub
 
Last edited:
Upvote 0
Hi, thanks for taking the time to reply, appreciate the assistance.
I have tried the suggested, it creates the folder but does not save the files into the folder - saves it outside.

Any thoughts?

Thank you
here you go

thank you for taking the time to present your code in a legible format

it makes it so much easier just to look at it on the webpage

Code:
Sub SaveWorksheetsAsPDFs()


    Dim sFile       As String
    Dim sPath       As String
    Dim wks         As Worksheet
 
    With ActiveWorkbook
        sPath = .Path & "\[COLOR=#ff0000]test123\[/COLOR]"
[COLOR=#ff0000]        MkDir sPath[/COLOR]
        For Each wks In .Worksheets
            If LCase(wks.Range("N2").Value) = "print" Then
                sFile = wks.Range("A6").Value & ".pdf"
                wks.ExportAsFixedFormat Type:=xlTypePDF, _
                                        Filename:=sPath & sFile, _
                                        Quality:=xlQualityStandard, _
                                        IncludeDocProperties:=False, _
                                        IgnorePrintAreas:=False, _
                                        OpenAfterPublish:=False
            End If
        Next wks
    End With
End Sub
 
Upvote 0
Try changing Dir to folder just created & see if that helps.
Also, may be good idea to wrap MkDir in an On error statement just in case Folder already exits:

Code:
On Error Resume Next
    MkDir sPath
    On Error GoTo 0
    ChDir sPath

Dave
 
Upvote 0

Forum statistics

Threads
1,203,758
Messages
6,057,184
Members
444,913
Latest member
ILGSE

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