Hi have 50+ sheets in my excel workbook and i want to save them individual pdf files.
I also want to save them in specific folder and want to name these files from certain cell.
I got the basic of basic, saving them individually to desktop, but having trouble saving to certain folder and name part.
Sub Save_As_PDF_To_Desktop_4()
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
If sh.Name <> "Instructions" And sh.Visible = True Then sh.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
CreateObject("WScript.Shell").specialfolders("Desktop") & "\" & sh.Name & ".pdf" _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
Next sh
End Sub
Could someone assist me on this? Thanks!
Related
I have
-worksheet name "sheet2:
-Datavalidation list in G1
looking if there's a way to have macro that cycles through the data validation list and exports a workbook with the name of the text in that cell and loop through so every item of list exports as their own workbook . aka if there's 100 lines in the drop down, then i'd end with 100 excel files each individually named for the 100 drop down options.
Example , This worked for me to print out pdfs :
Public Sub Create_PDFs()
Dim destinationFolder As String
Dim dataValidationCell As Range, dataValidationListSource As Range, dvValueCell As Range
destinationFolder = "C:\Users\DELL 04\Desktop\Q-Book Activities\Experiment" 'Same folder as workbook containing this macro
'destinationFolder = "C:\path\to\folder\" 'Or specific folder
If Right(destinationFolder, 1) <> "\" Then destinationFolder = destinationFolder & "\"
'Cell containing data validation in-cell dropdown
Set dataValidationCell = Worksheets("sheet2").Range("G1")
'Source of data validation list
Set dataValidationListSource = Evaluate(dataValidationCell.Validation.Formula1)
'Create PDF for each data validation value
For Each dvValueCell In dataValidationListSource
dataValidationCell.Value = dvValueCell.Value
With dataValidationCell.Worksheet.Range("A1:I45")
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=destinationFolder & dvValueCell.Value & ".PDF", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End With
Next
End Sub
What if you just use the same, working code, and create workbooks instead?
For Each dvValueCell In dataValidationListSource
Workbooks.Add.SaveAs Filename:=destinationFolder & dvValueCell
Workbooks(dvValueCell & ".xlsx").Close
Next
Try replacing of:
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=destinationFolder & dvValueCell.Value & ".PDF", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
with
Worksheets("sheet2").Copy 'it creates a new workbook with Sheet2 content
ActiveWorkbook.saveAs Filename:=destinationFolder & dvValueCell.Value & ".xlsx", FileFormat:=xlWorkbookDefault
ActiveWorkbook.Close False
I have the below script to export each excel sheet to a separate PDF page. It works fine, except for one small issue. I need the range Columns A:H to fit on one PDF page. How can I update the below script to make sure I export all information on one page for the specified columns?
Sub ExportToPDFs()
Dim ws As Worksheet
For Each ws In Worksheets
ws.Select
nm = ws.Name
ActiveSheet.Columns("A:H").ExportAsFixedFormat Type:=xlTypePDF, _
Filename:="C:\My Folder\" & nm & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
Next ws
End Sub
I would set the print area to A:H and set scale to fit to 1 page wide and 1 page high through the Page Setup part of the Ribbon. Then the following code should work without having to mess with the scaling through VBA.
Sub ExportToPDFs()
Dim ws As Worksheet
For Each ws In Worksheets
ws.Select
nm = ws.Name
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:="C:\My Folder\" & nm & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
Next ws
End Sub
I want to save my xls document as a PDF.
Some documents include the "Readme" sheet, some don't.
In this event, I set the condition leading to mandatory saving despite of the "Readme" sheet presence.
I found some hint here: Test or check if sheet exists
Sub sheet_exist()
Dim I As Long
For I = 1 To Worksheets.Count
If Worksheets(I).Name = "Readme" Then
Sheets("Readme").Visible = False
ThisWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & "\" & ThisWorkbook.Name, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
Sheets("Readme").Visible = True
Else
ThisWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & "\" & ThisWorkbook.Name, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
End If
Next I
End Sub
My PDF document is saved twice. How can I eliminate the second save?
Your for/next loop is currently structured in a way that it will save your sheet with every iteration (it will first check if the current sheet is "Readme" and then save the the workbook - for every sheet in the workbook). So if you have two sheets in your workbook, it will save your workbook twice.
You need to restructure your code, so it first checks for the existence of "Readme" and then saves the workbook once.
Sub sheet_exist()
Dim I As Long
Dim bReadmeExists As Boolean
'First, we loop through all sheets and check if "Readme" exists. If it does, we hide the sheet.
For I = 1 To Worksheets.Count
If Worksheets(I).Name = "Readme" Then
Sheets("Readme").Visible = False
'If readme exists, we need to make it visible again in the end
bReadmeExists = True
End If
Next I
'Now we export the workbook once
ThisWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & "\" & ThisWorkbook.Name, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
'Finally, we make the readme sheet visible again
If bReadmeExists Then
Sheets("Readme").Visible = True
End If
End Sub
I have also added a boolean variable to "remember" if the readme sheets exists (so we eventually can make it visible again after the export).
I also allowed my self to indent the code correctly, so it is easier to read.
Although this questions was already asked I did not find a real answer for it.
I need to programmatically select worksheets in a workbook and save them to PDF file WITHOUT creating temporary workbook and copying selected worksheets to it.
Alternatively how do create workbook without displaying it - that is, in memory and then on HD? This is again may help to solve the first question...
Try this:
Sub SaveToPdf()
Dim ws As Worksheet
Dim sfName As String
For Each ws In Worksheets
sfName = ws.Name
sfName = "C:\Misc\" & sfName & ".pdf"
ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
sfName, Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Next ws
End Sub
You'd need to make sure your print areas on each sheet were set first I think
I have a workbook with many worksheets. I would like to save as two-page PDFs, where the first page is Worksheet 1, and the second page is Worksheets 2-x. My code currently only allows me to save individual PDFs for each worksheet in the workbook. I am wondering what to add to it to make it do this. Can anyone share some advice?
Thanks!
Option Explicit
Sub createPDFfiles()
Dim ws As Worksheet
Dim Fname As String
For Each ws In ActiveWorkbook.Worksheets
On Error Resume Next
Fname = "C:\Folder\" & ws.Name & "Report" & Format(Date, "yyyy-mm-dd") & ".pdf"
ws.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=Fname, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False
Next ws
End Sub
You are enumerating through the worksheets and doing your save inside that loop. That is why you are getting one PDF per worksheet. Try using just workbook instead of ActiveWorkbook.Worksheets.
Gah. It was staring me in the face the whole time. I amended the code to include a selection, and named the second worksheet ws.Name. Final script looks like this:
Option Explicit
Sub createPDFfiles()
Dim ws As Worksheet
Dim Fname As String
For Each ws In ActiveWorkbook.Worksheets
On Error Resume Next
Fname = "C:\Folder\" & ws.Name & "Report" & Format(Date, "yyyy-mm-dd") & ".pdf"
Sheets(Array("Sheet1", ws.Name)).Select
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=Fname, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False
Next ws
End Sub
Thanks for your help everyone!