save sheet as pdf to subfolders based on cell value

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hi all
i search code save sheet as pdf to subfolder the directory is "C:\Users\OSE\Downloads\client \" i have main folder calls client and contains many subfolders customer1,customer2,customer3,customer4 when i fill in cell h2 = customer1 then save as pdf to subfolder customer1 and if i fill customer2 in h2 then save as pdf to subfolder customer2 and so on
i appreciate if anybody help
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Try:
VBA Code:
Sub SavePDF()
    ChDir "C:\Users\OSE\Downloads\client\" & ActiveSheet.Range("H2").Value & "\"
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ActiveSheet.Range("H2") _
        , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=False
End Sub
This code saves the file in the appropriate folder using the H2 as the file name.
 
Upvote 0
hi, mump yes it works but i have something to overcome error
i would if i fill in h2 nothing subfolder is existed then give me message the folder is not existed then the message gives me choice do you want a create subfolder if is ok then create if no then ignore it
 
Upvote 0
i have main folder calls client and contains many subfolders customer1,customer2,customer3,customer4
According to your description, the macro assumes that a subfolder for each customer already exists. Is this not the case?
 
Upvote 0
yes but i have found gives me error if i fill in h2 customer4 and there is no customer4 it gives me error i don't enforce you that if you did it is ok if not i will issue a new thread
thanks
 
Upvote 0
What do you want to happen if the sub folder doesn't exist?
 
Upvote 0
Try:
VBA Code:
Sub SavePDF()
    Application.ScreenUpdating = False
    Dim foldName As String, foldExists As String
    foldName = "C:\Users\OSE\Downloads\client\" & ActiveSheet.Range("H2").Value & "\"
    foldExists = Dir(foldName, vbDirectory)
    If foldExists <> "" Then
        ChDir "C:\Users\OSE\Downloads\client\" & ActiveSheet.Range("H2").Value & "\"
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ActiveSheet.Range("H2") _
            , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
            :=False, OpenAfterPublish:=False
    Else
        MkDir "C:\Users\OSE\Downloads\client\" & ActiveSheet.Range("H1").Value
        ChDir "C:\Users\OSE\Downloads\client\" & ActiveSheet.Range("H1").Value & "\"
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ActiveSheet.Range("H2") _
            , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
            :=False, OpenAfterPublish:=False
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
perfect mump actually i try amending about file name to make incrementing based on h1 = inv1000 so what i try when save file name based on h1 to become inv1000, inv1001 and so on it gives me error in this line
VBA Code:
Filename:=ActiveSheet.Range("h1").Value + 1
sorry about it this is last request
 
Upvote 0
Will the text in H1 always start with "inv" ?
 
Upvote 0

Forum statistics

Threads
1,214,657
Messages
6,120,769
Members
448,991
Latest member
Hanakoro

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