Save 2 Worksheets As PDF

leewoo5

New Member
Joined
Feb 5, 2017
Messages
13
Hi all,

I have the following code that I would like to adapt to change from saving one worksheet to saving two as a pdf.

The worksheet names are "NCR" and "RCA" and the code is as follows:

Code:
Sub SaveAsPDFExcel()






Dim fName As String
Dim sLoc As String 'location




    sLoc = "I:\FST\Quality\Non-Conformance\Non-Conformance Reports\Open\"




With ActiveSheet
    fName = .Range("L4").Value & "-" & .Range("E6").Value
    
    'Save as xlsx
    ActiveWorkbook.SaveAs Filename:=sLoc & fName, FileFormat:=xlOpenXMLWorkbookMacroEnabled
    
    .ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
            sLoc & fName, Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
            
            
End With
End Sub

Any help will be greatly appreciated.

Many thanks,
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hi,
Try this:
Rich (BB code):
Sub SaveAsPDFExcel()
 
  Dim fName As String
  Dim sLoc As String  'location
 
  sLoc = "I:\FST\Quality\Non-Conformance\Non-Conformance Reports\Open\"
 
  ' Select both sheets to export into PDF file
  Sheets(Array("NCR", "RCA")).Select
 
  With ActiveSheet
   
    fName = .Range("L4").Value & "-" & .Range("E6").Value
 
    'Save as xlsm
    ActiveWorkbook.SaveAs Filename:=sLoc & fName, FileFormat:=xlOpenXMLWorkbookMacroEnabled
   
    ' Export to the PDF
    .ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
                         sLoc & fName, Quality:=xlQualityStandard, _
                         IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
   
    ' Unselect the group (select only one sheet)
    .Select
 
  End With
 
End Sub
Regards
 
Last edited:
Upvote 0
ZVI

I'm simply trying to save as pdf.
I tried your code using my own paths and I'm getting an error,

Run-time error '1004':
Document not saved. The document may be open, or an error may have been encountered when saving.

Any suggestions?

Code:
Sub SaveAsPDFExcel()
 
  Dim fName As String
  Dim sLoc As String  'location
 
  sLoc = "C:\Users\Jason Barnes\Desktop\Denbury"
 
  ' Select sheet to export into PDF file
  Sheets("MAIN").Select
 
  With ActiveSheet
   
    fName = .Range("BC3").Value
 
    'Save as xlsm
    ActiveWorkbook.SaveAs Filename:=sLoc & fName, FileFormat:=xlOpenXMLWorkbookMacroEnabled
   
    ' Export to the PDF
    .ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
                         sLoc & fName, Quality:=xlQualityStandard, _
                         IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
                         
  End With
End Sub
 
Upvote 0
The backslash symbol (actuall a path separator) has been omitted at the end of sLoc. Try this modification of your code^
Rich (BB code):
Sub SaveAsPDFExcel()
 
  Dim fName As String
  Dim sLoc As String  'location
 
  ' Don't forger the backslash symbol "\" at the end of the location path !
  sLoc = "C:\Users\Jason Barnes\Desktop\Denbury\"
 
  ' Get file name
  fName = Sheets("MAIN").Range("BC3").Value
 
  'Save as xlsm
  ActiveWorkbook.SaveAs Filename:=sLoc & fName, _
                        FileFormat:=xlOpenXMLWorkbookMacroEnabled
 
  ' Disable screen updating
  Application.ScreenUpdating = False
 
  ' Select sheets for exporting into the PDF file
  Sheets(Array("NCR", "RCA")).Select
 
  ' Export to the PDF
  ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
                                  Filename:=sLoc & fName, _
                                  Quality:=xlQualityStandard, _
                                  IncludeDocProperties:=True, _
                                  IgnorePrintAreas:=False, _
                                  OpenAfterPublish:=False
 
  ' Select the MAIN sheet
  Sheets("MAIN").Select
 
  ' Restore screen updating
  Application.ScreenUpdating = True
 
End Sub
Regards
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,148
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