save sheet as pdf to subfolders based on cell value

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
985
Office Version
  1. 2010
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
 

Some videos you may like

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
10,238
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
 

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
985
Office Version
  1. 2010
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
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
10,238
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
 

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
985
Office Version
  1. 2010

ADVERTISEMENT

great work thanks for every thing i appreciate your efforts and your time
best regards
 

jefflab1

New Member
Joined
Jul 9, 2015
Messages
14

ADVERTISEMENT

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?
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
10,238
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
 

jefflab1

New Member
Joined
Jul 9, 2015
Messages
14
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
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
10,238
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,524
Messages
5,625,311
Members
416,093
Latest member
tjrogers04

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
Top