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
Related
I need to get both of these sheets and ranges to be combined into ONE PDF. I have tried all of the macros I can find and none of them work. Here is the Macro I'm working with, which all works except for the ranges being combined in one Doc
Private Sub SaveLHForms()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
FormName = Sheets("SETUP").Range("B2").Value & " " & ActiveSheet.Range("S1") & ".pdf"
ChDir DesktopAddress
Sheets("Lienholder Docs").Range("A45:I151").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
FormName, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
Sheets("Settlement Letters").Range("A47:I92").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
FormName, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
First, you need to create a pdf document after setting the print area in the page settings of each sheet.
Sub test()
Dim path As String
Dim myArr As Variant, a As Variant
Dim rngArr As Variant
Dim Ws As Worksheet
Dim formName As String
Dim i As Integer
formName = Sheets("SETUP").Range("B2").Value & " " & ActiveSheet.Range("S1") & ".pdf"
myArr = Array("Lienholder Docs", "Settlement Letters") '<~~ Sheet name
rngArr = Array("A45:I151", "A47:I92") '<~~ print area address
For i = 0 To UBound(myArr)
Set Ws = Sheets(myArr(i))
With Ws
.PageSetup.PrintArea = .Range(rngArr(i)).Address
End With
Next a
Sheets(myArr).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
formName, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
End Sub
This may work:
Sub Macro1()
Sheets("Lienholder Docs").Activate
ActiveSheet.Range("A45:I151").Select
Sheets("Settlement Letters").Activate
ActiveSheet.Range("A47:I92").Select
Sheets(Array("Lienholder Docs", "Settlement Letters")).Select
Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
FormName, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
End Sub
Based on:
Excel VBA to Export Selected Sheets to PDF
EDIT#1:
This version should un-do any grouping:
Sub Macro2()
Dim s As Worksheet
Set s = ActiveSheet
FormName = "C:\TestFolder\xxx.pdf"
Sheets("Lienholder Docs").Activate
ActiveSheet.Range("A45:I151").Select
Sheets("Settlement Letters").Activate
ActiveSheet.Range("A47:I92").Select
Sheets(Array("Lienholder Docs", "Settlement Letters")).Select
Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
FormName, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
s.Activate
End Sub
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 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.
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 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