I'm trying to create separate PDFs for each sheet in a selection of sheets, with a name determined by the sheet name and the contents of one cell.
The code is as follows:
Sub SaveWorksheetsAsPDFs()
Dim sFile As String
Dim sPath As String
Dim sh As Object
Dim InvDate As String
With ActiveWorkbook
sPath = .Path & "\"
For Each sh In ActiveWindow.SelectedSheets
InvDate = Format(Range("G9"), "dd-mm-yy")
sFile = sh.Name & " - Invoice - " & InvDate & ".pdf"
sh.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=sPath & sFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Next sh
End With
End Sub
Separate PDFs are created and named correctly, however, the PDF contains all the selected sheets instead of one.
With my experience, i've seen that the ExportAsFixedFormat always exports all selected sheets.
You can come around this by saying sh.Select after your For Each, and then it should works as you describe in the OP.
Edited code:
Sub SaveWorksheetsAsPDFs()
Dim sFile As String
Dim sPath As String
Dim sh As Object
Dim InvDate As String
With ActiveWorkbook
sPath = .Path & "\"
For Each sh In ActiveWindow.SelectedSheets
sh.Select '<----- New LINE
InvDate = Format(Range("G9"), "dd-mm-yy")
sFile = sh.Name & " - Invoice - " & InvDate & ".pdf"
sh.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=sPath & sFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=True, _
OpenAfterPublish:=False
Next sh
End With
End Sub
Hope this helps you achive your goal. :)
Related
I have never really worked with VBA and I am getting stuck on a simple problem.
Essentially I have a workbook with multiple worksheets. I Have a Macro which prints all worksheets to pdf. I want to change the macro to only print specific worksheets
Code sofar:
Sub Print_All_pages()
Dim ws As Worksheet
For Each ws In Worksheets
ws.Select
nm = ws.Name
Dim Filepath As String
Filepath = Range("Destination").Value & "\Übersicht Bonus - " & Range("Month").Value & " " & Range("Year").Value & " - " & nm & ".pdf"
Range("A1:N35").ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=Filepath, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=True, _
OpenAfterPublish:=False
Next ws
Worksheets("Main Sheet").Activate
End Sub
I hav tried the following which doesn't work
For Each ws in Worksheets
If ws.Name = "Textbausteine Mail" Or ws.Name = "Overzichten verzenden" Or ws.Name = "Übersicht_Januar" Then
'Do nothing
Else
nm = ws.Name
Dim Filepath As String
Filepath = Range("Destination").Value & "\Übersicht Bonus - " & Range("Month").Value & " " & Range("Year").Value & " - " & ws.Name & ".pdf"
Range("A1:N35").ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=Filepath, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=True, _
OpenAfterPublish:=False
Next ws
Worksheets("Main Sheet").Activate
This however throws me the error compilation error Next without for
Here is the workbook setup:
Any help is greatly appreciated
Export Range to PDF
A Quick Fix
Option Explicit
Sub ExportToPDF()
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim FilePathLeft As String
With wb.Worksheets("Main Sheet")
FilePathLeft = .Range("Destination").Value & "\Übersicht Bonus - " _
& .Range("Month").Value & " " & .Range("Year").Value & " - "
End With
Dim ws As Worksheet
Dim FilePath As String
For Each ws In wb.Worksheets
Select Case ws.Name
' do NOT export:
Case "Textbausteine Mail", "Overzichten verzenden", "Übersicht_Januar"
' do export:
Case Else
FilePath = FilePathLeft & ws.Name & ".pdf"
ws.Range("A1:N35").ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=FilePath, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=True, _
OpenAfterPublish:=False
End Select
Next ws
End Sub
I need to export excel sheet as PDF named with String and Cell Value for Example XXXY - Cell Value
Note: i do not need codes that uses path location for the saving file
I tried the following code
Sub IVI_Formatting_Export_Two_PDF ()
Dim strFilename As String
Dim srn As String Dim SWPDF As Worksheet
Set SWPDF = ThisWorkbook.Sheets ("BSS CPES MainPage")
srn = "CPE Main-Page"
strFilename = SWPDF.Range ("F7").Value
Create File name with Warehouse Name
Export2PDF
SWPDF.ExportAsFixedFormat
Type:=xlTypePDF,
Filename:=strFilename & srn,
Quality:=xl QualityStandard,
IncludeDocProperties:=False,
IgnorePrintAreas:=False,
From:=1,
To:=1,
OpenAfterPublish:=True
End Sub
You must specify a path to export PDF. Without path where it will be saved? You can use variable to specify path. Currently I have used same as file path. Your problem was in filename parameter. Try below sub.
Sub IVI_Formatting_Export_Two_PDF()
Dim strFilename As String
Dim srn As String
Dim SWPDF As Worksheet
Set SWPDF = ThisWorkbook.Sheets("BSS CPES MainPage")
srn = "CPE Main-Page"
strFilename = SWPDF.Range("F7").Value
'Create File name with Warehouse Name
'Export2PDF
SWPDF.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=ThisWorkbook.Path & "\" & strFilename & "_" & srn & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
From:=1, _
To:=1, _
OpenAfterPublish:=True
'Clear memory
Set SWPDF = Nothing
End Sub
Here Filename:=ThisWorkbook.Path & "\" & strFilename & "_" & srn & ".pdf" will export the PDF to same folder where your workbook is located. Also you must include .pdf extension to export excel sheet as pdf.
I am trying to convert an excel sheet to pdf than save it to a folder on my desktop, however, I keep getting a run time error of 1004 - application-defined or object defined error. I have placed the code below. Can anyone please assist?
Private Sub pdfAlternate()
Dim strPathLocation As String
Dim strFilename As String
Dim strPathFile As String
Dim fileSaveName As String
Dim fileSavePath As String
Dim pdfOpenAfterPublish As Boolean
strPathLocation = Worksheets("Form").Range("C11").Value
strFilename = Worksheets("Form").Range("L11").Value
fileSaveName = strPathLocation & "_" & strFilename
fileSavePath = "C:\Users\fou55227\Desktop\BMW" & "\" & fileSaveName & "_" & Format(Date, "MM.DD.YYYY") & ".pdf"
ActiveWorkbook.Sheets("Form").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
fileSavePath _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=True
Debug.Print fileSaveName
Debug.Print fileSavePath
End Sub
So I'm using this macro to save individual excel worksheets as their own PDF when run:
Sub SaveWorksheetsAsPDFs()
Dim sFile As String
Dim sPath As String
Dim wks As Worksheet
With ActiveWorkbook
sPath = .Path & "\"
For Each wks In .Worksheets
sFile = wks.Name & ".pdf"
wks.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=sPath & sFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Next wks
End With
End Sub
I would just like to add to it to not start from a certain worksheet or to exclude certain worksheets. Any clue how?
You can use Select Case to specify sheetnames to ignore, as shown. Simply replace the "IgnoreSheet1", "IgnoreSheet2" with the actual sheet names you want skipped. It's just a comma delimited list, so add as many as you want.
Sub SaveWorksheetsAsPDFs()
Dim sFile As String
Dim sPath As String
Dim wks As Worksheet
With ActiveWorkbook
sPath = .Path & "\"
For Each wks In .Worksheets
Select Case wks.Name
Case "IgnoreSheet1", "IgnoreSheet2" 'Do nothing
Case Else
'Code here to run on the other sheets
sFile = wks.Name & ".pdf"
wks.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=sPath & sFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End Select
Next wks
End With
End Sub
I have cobbled together a VBA script that loops through a list of data, changing the value of a single cell on a summary page. That cell drives a number of formulas. After each iteration, the cell range of interest is saved off as a PDF.
I am looking to avoid having to manually hit enter every time the 'save as' dialog box is generated on each loop. Once I deploy this script, I could be looking at 1k+ iterations.
Sub AlterID()
Dim ws As Worksheet
Dim strPath As String
Dim myFile As Variant
Dim strFile As String
Set ws = Worksheets("Summary Data")
For Each c In Worksheets("Data").Range("A2:A11").Cells
Worksheets("Summary Data").Range("B1").Value = c.Value
strFile = ws.Range("D3").Value
strFile = ThisWorkbook.Path & "\" & strFile
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")
If myFile <> "False" Then
ws.Range("D3:H9").Select
Selection.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=myFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End If
Next
End Sub
Sub AlterID()
Dim ws As Worksheet, c As Range
Dim strFile As String
Set ws = Worksheets("Summary Data")
For Each c In Worksheets("Data").Range("A2:A11").Cells
ws.Range("B1").Value = c.Value
strFile = ThisWorkbook.Path & "\" & ws.Range("D3").Value
ws.Range("D3:H9").ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=strFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Next
End Sub
Have you tried turning off application alerts?
Application.DisplayAlerts = False 'before your code
Application.DisplayAlerts = True 'after your code
Edit 1
Here is a sub I use to save a file to a PDF
Sub SaveAsPDF()
Dim myValue As Variant
Dim mySheets As Variant
mySheets = Array("Report")
For Each sh In mySheets
Sheets(sh).PageSetup.Orientation = xlLandscape
Next
uid = InputBox("Enter your UID")
uid = StrConv(uid, vbProperCase)
Application.PrintCommunication = False
With Sheets("Report").PageSetup
.FitToPagesWide = 1
.FitToPagesTall = False
End With
Application.PrintCommunication = True
Dim fName As String
fName = "HMB SOX report for week ending " & ActiveSheet.Range("H4").Text
Call selectPage("Report")
With ActiveSheet
.ExportAsFixedFormat Type:=xlTypePDF, fileName:= _
"C:\Users\" & uid & "\Desktop\" & fName, :=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, Publish:=False
End With
End Sub