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

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
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:=Left(ActiveSheet.Range("H1"), 3) & Mid(ActiveSheet.Range("H1"), 4, 99999) + 1 _
            , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
            :=False, OpenAfterPublish:=False
    Else
        MkDir "C:\Users\OSE\Downloads\client\" & Left(ActiveSheet.Range("H1"), 3) & Mid(ActiveSheet.Range("H1"), 4, 99999) + 1
        ChDir "C:\Users\OSE\Downloads\client\" & Left(ActiveSheet.Range("H1"), 3) & Mid(ActiveSheet.Range("H1"), 4, 99999) + 1 & "\"
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Left(ActiveSheet.Range("H1"), 3) & Mid(ActiveSheet.Range("H1"), 4, 99999) + 1 _
            , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
            :=False, OpenAfterPublish:=False
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
thanks a gain but what i want every time when run macro it supposes incrementing based on h1 i tested your updating code but it keep in this numbering inv1001 not change when i run macro repeatedly should be inv1002,inv1003 and so on
 
Upvote 0
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:=Left(ActiveSheet.Range("H1"), 3) & Mid(ActiveSheet.Range("H1"), 4, 99999) + 1 _
            , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
            :=False, OpenAfterPublish:=False
        ActiveSheet.Range("H1") = Left(ActiveSheet.Range("H1"), 3) & Trim(Mid(ActiveSheet.Range("H1"), 4, 99999) + 1)
    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:=Left(ActiveSheet.Range("H1"), 3) & Mid(ActiveSheet.Range("H1"), 4, 99999) + 1 _
            , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
            :=False, OpenAfterPublish:=False
        ActiveSheet.Range("H1") = Left(ActiveSheet.Range("H1"), 3) & Trim(Mid(ActiveSheet.Range("H1"), 4, 99999) + 1)
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
great work thanks for every thing i appreciate your efforts and your time
best regards
 
Upvote 0
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.


I've copied the code and getting an error. Here is my code with the item's changed of course.

Private Sub CommandButton1_Click()

ChDir "S:\Fire Dispatchers\Circuits Program\Reading_Data\" & ActiveSheet.Range("C2:C3").Value & "\"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ActiveSheet.Range("C2:C3") _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
End Sub


What am I doing wrong?
 
Upvote 0
Try:
VBA Code:
Private Sub CommandButton1_Click()
    ChDir "S:\Fire Dispatchers\Circuits Program\Reading_Data\" & ActiveSheet.Range("C2").Value & ActiveSheet.Range("C3").Value & "\"
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ActiveSheet.Range("C2").Value & ActiveSheet.Range("C3").Value _
    , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, OpenAfterPublish:=False
End Sub
 
Upvote 0
Try:
VBA Code:
Private Sub CommandButton1_Click()
    ChDir "S:\Fire Dispatchers\Circuits Program\Reading_Data\" & ActiveSheet.Range("C2").Value & ActiveSheet.Range("C3").Value & "\"
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ActiveSheet.Range("C2").Value & ActiveSheet.Range("C3").Value _
    , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, OpenAfterPublish:=False
End Sub


It's giving me another error -

Run-time error '76':
Path not found

But that is the path and folder where I'd like to save it. Is it a permissions issue? We can save and change things freely, so I highly doubt it.

Thanks for helping me with this. It really means alot! - Jeff
 
Upvote 0
It looks like the folder name and the file name are made up of the contents of C2 and C3. If that is true, post an example of the contents of C2 and C3 and what the full path would look like using those values.
 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,726
Members
448,987
Latest member
marion_davis

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