I'm attempting to output 4 sheets into one pdf file, but for some reason the output only includes the first sheet, "Report 1a". Here's my code:
Dim Ref As Worksheet
Set Ref = Worksheets("Charts for Report")
Sheets(Array("Report 1a", "Report 1b", "Report 2", "Comments")).Select
Sheets("Report 1a").Activate
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"T:\QA\Sample Reports\Reports for CCC\" & Ref.[B1] _
& " - " & Ref.[B2] & " - " & Worksheets("Provider Data").[I2] & ".pdf" _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
Worksheets("Report 1a").Select
I've even tried recording a macro to see what I'm doing wrong. It'll work (i.e., include all 4 pages) when I record it, but if I try to rerun it it'll only put include the first page ("Report 1a").
I still don't know what was going wrong, but I at least found a solution.
Prior to exporting anything I hid all the worksheets that I didn't want included. (All the more reason to avoid select like the plague!)
Sub Hide_non_Packet_Sheets()
Dim index As Integer
Dim SheetExists As Worksheet
For index = 1 To 50
Set SheetExists = Sheets(index)
On Error Resume Next
If Sheets(index).Name = "Page1" Or Sheets(index).Name = "Page2" Then
Sheets(index).Visible = True
Else
Sheets(index).Visible = False
End If
Next index
End Sub
Then I just export the entire workbook:
ThisWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= "C:\Place\Name.pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas :=False, _
OpenAfterPublish:=False
And then you can set a macro to unhide sheets when you're done exporting if you want.
Related
I have a macro that creates individual PDFs for a set of tabs. Is there a way to add a step and combine all the PDFs into one file?
Sub Print_Exhibit()
Dim Numb_Exhibit As Double
Dim File_Location As String
Dim Sheet_Name As String
Dim X As Double
Dim Y As Double
Numb_Exhibit = WorksheetFunction.Max(Sheets("Control - Exhibit
Key").Range("B:B"))
File_Location = Sheets("Control - Exhibit Key").Range("K6").Value
For X = 1 To Numb_Exhibit
Y = 8 + X
Sheet_Name = Sheets("Control - Exhibit Key").Range("E" & Y).Value
Sheets(Sheet_Name).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:=File_Location
& "\" & Sheet_Name & ".pdf" _
, Quality:=xlQualityStandard, IncludeDocProperties:=True,
IgnorePrintAreas _
:=False, OpenAfterPublish:=True
Next
End Sub
Thank you so much for your help!
Loop through all tabs, copy and paste into a new common tab. Export it.
Did you do a prerequisite Google search before posting here?
Save multiple sheets to .pdf
Public Sub subCreatePDF()
If Not IsPDFLibraryInstalled Then
'Better show this as a userform with a proper link:
MsgBox "Please install the Addin to export to PDF. You can find it at http://www.microsoft.com/downloads/details.aspx?familyid=4d951911-3e7e-4ae6-b059-a2e79ed87041".
Exit Sub
End If
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=ActiveWorkbook.Path & Application.PathSeparator & _
ActiveSheet.Name & " für " & Range("SelectedName").Value & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
Private Function IsPDFLibraryInstalled() As Boolean
'Credits go to Ron DeBruin (http://www.rondebruin.nl/pdf.htm)
IsPDFLibraryInstalled = _
(Dir(Environ("commonprogramfiles") & _
"\Microsoft Shared\OFFICE" & _
Format(Val(Application.Version), "00") & _
"\EXP_PDF.DLL") <> "")
End Function
OR
ThisWorkbook.Sheets(Array("Sheet1", "Sheet2")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\tempo.pdf", Quality:= xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
ALSO
https://danwagner.co/how-do-i-save-multiple-sheets-as-a-single-pdf/
I'm trying to create VBA code that moves down a list, taking each of those cells and inputting their value to B2 (this value the name of a tutoring service). The value in B2 is then used by other cells to find reference values. I'd like each iteration of i (1 to 91) to save the new information as a PDF so that I can easily send those documents out.
My problem is that I cannot get the file to save, I receive Run-Time error 9 Subscript out of Range. The loop works exactly as I want.
How do I save as a PDF while looping through a range of values?
Sub moveselection()
Dim i As Integer
For i = 1 To 91
Range("B2").Value = Range("H2").Offset(i, 0).Value
ThisFile = Range("B2").Value
Sheets("Parents").Ranges("A1:F16").ExportAsFixedFormat Type:=xlTypePDF,
Filename:= _
"H:\Projects\Nathan\ProviderPDF\ & ThisFile.pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Next i
End Sub
Couple of changes:
Sub moveselection()
Dim i As Integer
For i = 1 To 91
Range("B2").Value = Range("H2").Offset(i, 0).Value
ThisFile = Range("B2").Value
Sheets("Parents").Range("A1:F16").ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:= "H:\Projects\Nathan\ProviderPDF\" & ThisFile & ".pdf", _
Quality:= xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
Next i
End Sub
If it still errors then please indicate which line gives the error
i am an amateur programmer learning how to program using vba
without further a due, my question is :
i have created a listbox (listbox1) where i've listed all my sheets(ws) name in a specific workbook.
i've also created another listbox(listbox2) where when i select some sheets name on the listbox1, they will be transfer to listbox2.
my main objective is that by choosing one or more sheets listed in the listbox2, and by clicking a button, i'll manage to save all the selected sheets in one pdf file.
here is the code for the button used to export the file in pdf that i've written but i've only managed to export them not in one pdf file but in numerous amount of pdf file.
Dim NomTableau() As String
For Each WkbkName In application.Workbooks()
If WkbkName.Name = choix_poteau.Value & "_" & section & "_" & projet & ".xlsx" Then
WkbkName.Activate
GoTo lois
End If
Next
Set wbk = Workbooks.Open(add1 & "\" & Me.projet.Value & "\" & Me.section.Value & "\poteaux\" & Me.choix_poteau.Value & "_" & Me.section & "_" & Me.projet & ".xlsx")
lois:
For i = 0 To ListBox2.ListCount - 1
While ListBox2.List(i) <> ""
Dim ws As Worksheet
For Each ws In Worksheets
If ws.Name = ListBox2.List(i) Then
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Export\Resultats__" & ListBox2.List(i - counter) & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
True
End If
Next
Wend
Next i
End Sub
thanks alot, really appreciate your help
Instead of exporting each sheet individually, select them all first and then call the ExportAsFixedFormat method.
Here is my test code, which worked as expected:
Option Explicit
Private Sub TestPDF()
Dim i As Integer
Dim arrSheets() As String
Dim strSheets As String
'Get our sheet names
For i = 1 To 3
strSheets = Worksheets(i).Name & "," & strSheets
Next
'Trim the trailing comma
strSheets = Left(strSheets, Len(strSheets) - 1)
arrSheets = Split(strSheets, ",")
ThisWorkbook.Sheets(arrSheets).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Users\qzbcjs\Documents\Useful Workbooks\test.pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
True
End Sub
I declared an array of strings, populated a string variable with the comma-delimited names of the worksheets (in my case I just wanted the first through third sheets), split that comma-delimited string into an array and used the array to select all of the desired sheets and only called the ExportAsFixedFormat method once.
Adapting this method to your code, beginning as the lois part would look something like this:
lois:
Dim ws As Worksheet
Dim arrSheets() As String
Dim strWs As String
For i = 0 To ListBox2.ListCount - 1
While ListBox2.List(i) <> ""
For Each ws In Worksheets
If ws.Name = ListBox2.List(i) Then
strWs = ws.Name & "," & strWs
End If
Next
Wend
Next i
strWs = Left(strWs, Len(strWs) - 1)
arrSheets = Split(strWs, ",")
ThisWorkbook.Sheets(arrSheets).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Export\Resultats__" & ListBox2.List(i - counter) & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
True
I have this problem trying to print multiple sheets in a single pdf. Browsing online and in the forum i found this code but when i use it i get the ERROR 9 "Subscript out of range" and I don't understand why. I tried in a new workbook the code and it works properly. Can someone help me?
Private Sub cmd_PrintPDF_Click()
ThisWorkbook.Sheets(Array("Costs", "Cars")).Select
Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & "/" & "Cost&Car", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
This macro runs from the main panel of my workbook, where there are various Command buttons that direct you in requested sheets.When i run this macro the sheet "Cars" is hidden, may be is this the problem? I tried with an .Activate before the code but it still doesn't work.
I use this code for export sheets to pdf. Maybe will be useful for you.
Sub SheetsToPdf()
Dim Arr() As String
Dim PdfFileName As String
PdfFileName = "Cost&Car"
ReDim Arr(1, 1)
'sheets name in 1st column, 2nd column for info about visibility sheets
Arr(0, 0) = "Costs"
Arr(1, 0) = "Cars"
Cells(1, 1).Select
For i = LBound(Arr, 1) To UBound(Arr, 1)
Arr(i, 1) = ThisWorkbook.Sheets(Arr(i, 0)).visible ' info about visibility sheets
If Arr(i, 1) = "0" Then 'check visible Sheets "-1" - visible = True, "0" - visible = False
ThisWorkbook.Sheets(Arr(i, 0)).visible = True
OrgVisible = False
End If
If i = 0 Then
ThisWorkbook.Sheets(Arr(i, 0)).Select
Else
ThisWorkbook.Sheets(Arr(i, 0)).Select False 'select all sheets with names in arr()
End If
Next i
'select all data
Cells.Select
'export to pdf
Selection.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
ThisWorkbook.path & "/" & PdfFileName, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
'restore old visibility for sheets
For i = LBound(Arr, 1) To UBound(Arr, 1)
If Arr(i, 1) = "0" Then 'set old visible
ThisWorkbook.Sheets(Arr(i, 0)).visible = False
End If
Next i
End Sub
Maybe this simpler version you need? Exports only visible sheets:
Sub SheetsToPdf2()
Dim PdfFileName As String
PdfFileName = "Cost&Car"
Cells(1, 1).Select
For Each Sheets_ In Sheets
If Sheets_.visible Then
ThisWorkbook.Sheets(Sheets_.Name).Select False
End If
Next
'select all data in one sheet
Cells.Select
'export to pdf
Selection.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
ThisWorkbook.path & "/" & PdfFileName, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
Indeed, you can't print an hidden sheet
And here is your code without the useless and ressource-greedy Select :
Private Sub cmd_PrintPDF_Click()
ThisWorkbook.Sheets("Costs").Visible = True
ThisWorkbook.Sheets("Cars").Visible = True
ThisWorkbook.Sheets(Array("Costs", "Cars")).ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=ThisWorkbook.Path & "/" & "Cost&Car", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
ThisWorkbook.Sheets("Costs").Visible = False
ThisWorkbook.Sheets("Cars").Visible = False
End Sub
I need do resize several sheets in order to get them always on the same size...and then save all of them as a PDF file. I do not know VBA so I'm doing a "frankestein code" getting pieces here and there.
What I get is the following code, that works for T1 sheet, but not for the other T2 and T3 sheets.How can I include them??
Sub Imprimir_PDF()
Dim ws As Worksheet
Dim lastRow As Long
Set ws = ThisWorkbook.Sheets("T1")
'find the last row with formatting, to be included in print range
lastRow = ws.UsedRange.SpecialCells(xlCellTypeLastCell).Row
ws.PageSetup.PrintArea = ws.Range("A2:CK" & lastRow).Address
ThisWorkbook.Sheets(Array("T1", "T2", "T3")).Select
Selection.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="C:\PaintChecker\Camadas\temp.pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End Sub
This takes your code, and substitutes the part where you get the last Row with a short loop and set all three sheets print area in the loop.
Then prints all three out into the same PDF as is indicated by your select statement of the array.
untested:
Sub Imprimir_PDF()
Dim lastRow As Long
Dim count As Long
Dim sheet As String
'find the last row with formatting, to be included in print range
For count = 1 To 3
sheet = "T" & count
lastRow = Sheets(sheet).UsedRange.SpecialCells(xlCellTypeLastCell).row
Sheets(sheet).PageSetup.PrintArea = Sheets(sheet).Range("A2:CK" & lastRow).Address
Next count
ThisWorkbook.Sheets(Array("T1", "T2", "T3")).Select
Selection.ExportAsFixedFormat _
Type:=xlTypePDF, _
fileName:="C:\PaintChecker\Camadas\temp.pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End Sub
Here's one way to do this:
Sub MakePDFs()
Dim sheetsToTouch As Variant
sheetsToTouch = Array("T1", "T2", "T3")
Dim sheetName As Variant
For Each sheetName In sheetsToTouch
Sheets(sheetName).Activate
ActiveSheet.PageSetup.PrintArea = ActiveSheet.Range("A2:CK" & ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row).Address
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="C:\PaintChecker\Camadas\" & sheetName & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
Next
End Sub