I recorded a macro.
It works if all of the sheets are visible but when I hide a sheet it will not save to pdf.
This is the code.
Sub save_pdf()
'
' save_pdf Macro
'
'
Sheets(Array("TITLE", "CML", "CLUSTER", "ORS", "MOBILE", "YPS", "DEVICES", "PORTS")).Select
Sheets("TITLE").Activate
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, fileName:= _ Sheets("MAIN").Range("customer_name") + " - Project Initiation_ Document.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True,_ IgnorePrintAreas:=False, OpenAfterPublish:=True
Sheets("MAIN").Select
End Sub
Something like this:
Sub ExportVisible()
Dim shts, sht As Worksheet, s, i As Long
shts = Array("TITLE", "CML", "CLUSTER", "ORS", "MOBILE", "YPS", "DEVICES", "PORTS")
i = 0
For Each s In shts
Set sht = ActiveWorkbook.Sheets(s)
If sht.Visible = xlSheetVisible Then
i = i + 1
sht.Select (i = 1) '"replace" parameter true when i=1
End If
Next s
'Sheets("TITLE").Activate '<<EDIT: remove this
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=Sheets("MAIN").Range("customer_name").Value & _
" - Project Initiation_Document.pdf ", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
It's likely due to the use of .Select. We want to avoid using .Select wherever possible. Instead, just set a loop to go through each worksheet in your workbook.
Sub save_PDFs()
Dim sht As Worksheet
For Each sht In ActiveWorkbook.Worksheets
sht.ExportAsFixedFormat Type:=xlTypePDF, fileName:=Sheets("MAIN").Range("customer_name") + " - Project Initiation_ Document.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
Next sht
End Sub
However, if you want only visible sheets, then do this:
Sub save_PDFs()
Dim sht As Worksheet
For Each sht In ActiveWorkbook.Worksheets
If sht.Visible = True Then sht.ExportAsFixedFormat Type:=xlTypePDF, fileName:=Sheets("MAIN").Range("customer_name") + " - Project Initiation_ Document.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
Next sht
End Sub
Related
I have the below code, and I am here to ask what I would need to add to the code to print pages 1 to 3 only?
Sub PrintVisa()
Dim invoiceRng As Range
Dim pdfile As String
'Setting range to be printed
Set invoiceRng = Range("C7:L175")
pdfile = " Seabourn_Visa_Letter"
invoiceRng.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=pdfile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=True, _
OpenAfterPublish:=True
Please, try the next code:
Sub ExportFirstThreeSheets()
Dim FileName As String
FileName = ThisWorkbook.Path & "\MyThreeSheets.pdf"
Sheets(Array(1, 2, 3)).copy 'it creates a new workbook containing the first three sheets
With ActiveWorkbook
.ExportAsFixedFormat xlTypePDF, FileName, , , , , , True
.Close False
End With
End Sub
The ExportAsFixedFormat has a page range built in. See below:
Sheet5.ExportAsFixedFormat Type:=xlTypePDF, Filename:="YouFileName.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=False, IgnorePrintAreas:=False, From:=1, To:=3, OpenAfterPublish:=True
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 would like to eligible my PDF document to save, although within the sheets with specified color.
How can I do that?
Sub SavecolorTabtoPDF()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If Tab Color = "Blue, Accent 1, Darker 25%" Then
'export as pdf
ws.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="C:\" & Left(ws.Name, Len(ws.Name) - 2) & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End If
Next ws
End Sub
You want to loop through the sheets and create an array of sheets based on tab color.
This will create your array of sheets, then pdf them as one.
Sub MakeArraySheets()
Dim sh As Worksheet
Dim ArraySheets() As String
Dim x As Variant
For Each sh In ActiveWorkbook.Worksheets
If sh.Tab.ColorIndex = 55 Then
ReDim Preserve ArraySheets(x)
ArraySheets(x) = sh.Name
x = x + 1
End If
Next sh
Sheets(ArraySheets).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & "\" & ThisWorkbook.Name, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
dim wsheet as worksheet
For Each wsheet In Thisworkbook.Worksheets
if wsheet.tab.colorindex = 1 then
Worksheet.SaveAs(*parameters*)
end if
next wsheet
EDIT: The colorindex for your specified color is 55. Find out by filling a cell with that color and running "mgbox activecell.interior.colorindex"
EDIT2: Removed "interior" before colorindex
I'm trying to loop through all active sheets and save them as separate PDFs.
dim ws as worksheet
dim path as string
...
For Each ws In ActiveWindow.SelectedSheets
ws.ExportAsFixedFormat _
xlTypePDF, _
Filename:=path & ws.Name, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenafterPublish:=False
Next
It kind of works:
A PDF file is created for each selected sheet in the correct folder... but...
The contents of those PDF files are all the same. It is the Active Sheet being printed each time but with a different filename.
How to fix this? Keep in mind, I only want selected sheets to print.
EDIT: I'm too new to upvote. Thanks for your answers!
You need to Select the sheet before printing out.
Just add the command ws.Select right before ws.ExportAsFixedFormat and it will work:
For Each ws In ActiveWindow.SelectedSheets
ws.Select '<-- the only thing you have to add.
ws.ExportAsFixedFormat _
xlTypePDF, _
Filename:=path & ws.Name, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenafterPublish:=False
Next
Apart for this:
Tip 1: to avoid seeing the sheets jumping over and over, you can add Application.ScreenUpdating = False before the loop starts and then setting it back to Application.ScreenUpdating = True at the end of the loop.
Tip 2: for user friendliness, you can get the currently active sheet at the beginning of the loop with Dim currentlySelectedSheet As Worksheet: Set currentlySelectedSheet = ActiveSheet and when the loop ends, you just select it back with currentlySelectedSheet.Select so that the user won't see anything change in their screen when running your macro.
Option Explicit
Sub Save_SelectedSheets_AsPdf()
Dim ws As Worksheet
Dim path As String
Dim actSheet As Worksheet
'...
Set actSheet = ActiveSheet
For Each ws In ActiveWindow.SelectedSheets
ws.Select
ws.ExportAsFixedFormat _
xlTypePDF, _
Filename:=path & ws.Name, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenafterPublish:=False
Next
actSheet.Select
End Sub
Sub Create_5_Sheets_and_name_them()
Dim iCt As Integer
For iCt = 2 To 5
Sheets.Add after:=Sheets(Sheets.Count)
ActiveSheet.Name = "Sh" & iCt
Range("A1").Value = ActiveSheet.Name
Next iCt
End Sub
you can specify it like this before you export it to pdf
How can I pdf multiple sheets in my Workbook into one pdf in landscape format? Here is what I have. I am missing the landscape syntax -
Sub CompileReport()
Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="F:\Report\Test" & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False
End Sub
Thanks!
Try this one:
Sub CompileReport()
Dim mySheets As Variant, sh
mySheets = Array("Sheet1", "Sheet2", "Sheet3")
For Each sh In mySheets
Sheets(sh).PageSetup.Orientation = xlLandscape
Next
Sheets(mySheets).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="F:\Report\Test" & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False
End Sub