Save sheets as pdf - vba

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

Related

vArray if else macro

I am trying to create a macro the adds different pages to a pdf based on a check box status. This is what I currently have.
Sub ToyotaMO()
'
' ToyotaMO Macro
'
Sheets("TC").Visible = True
Sheets("BS").Visible = True
Sheets("ToyotaMO").Activate
If Sheets("ACM").OLEObjects("Toyota").Object.Value = True Then
vArray = Array("ToyotaMO", "TC", "BS")
Else
vArray = Array("Proposal", "TC")
End If
ThisWorkbook.Sheets(vArray).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & "\" & ActiveSheet.Range("C5").Value & " Toyota Material Only ACM Proposal" & Format(Date, " MMDDYY") & ".pdf" _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=True
Sheets("ToyotaMO").Activate
Range("A1").Select
Sheets("TC").Visible = False
Sheets("BS").Visible = False
Sheets("ACM").Select
Range("B1").Select
End Sub
The macro works on all the other forms the only difference is I need to add worksheet "BS" to the PDF if the checkbox is checked. VBA always stalls at ThisWorkbook.Sheets(vArray).Select.
Any help is greatly appreciated
Try the following, this will loop through your array and do what you expect:
Sub ToyotaMO()
'
' ToyotaMO Macro
'
Sheets("TC").Visible = True
Sheets("BS").Visible = True
Sheets("ToyotaMO").Activate
If Sheets("ACM").OLEObjects("Toyota").Object.Value = True Then
vArray = Array("ToyotaMO", "TC", "BS")
Else
vArray = Array("Proposal", "TC")
End If
For i = LBound(vArray) To UBound(vArray)
ThisWorkbook.Sheets(vArray(i)).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & "\" & ActiveSheet.Range("C5").Value & " Toyota Material Only ACM Proposal" & Format(Date, " MMDDYY") & ".pdf" _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=True
Sheets("ToyotaMO").Activate
Range("A1").Select
Sheets("TC").Visible = False
Sheets("BS").Visible = False
Sheets("ACM").Select
Range("B1").Select
Next i
End Sub

Saving Multiple Ranges on two different sheets to PDF using VBA

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

VBA doesn't include every sheet in pdf output

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.

How to save a file with the location and name as cell values using VBA

Could someone please tell me what I am doing wrong, I am pretty new to VBA and have the following code. I want to open several excel files and save the "Cash" tab as a pdf to a specific folder. The problem I am having is that it tries to save the folder to the "Test" folder and not the folder indicated by the "Cells(r,3)" so I get an error after it saves the first PDF file because they have the same name. Any help would be appreciated!
Sub Cash_PDF_()
r = 2
Do While Cells(r, 5) <> ""
Workbooks.Open FileName:="H:\Investment\Fund Folders\" & Cells(r, 3) & "\" & Cells(r, 5), _
ReadOnly:=True, UpdateLinks:=0
Sheets("Cash").Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:="C:\Users\Desktop\Test\" & Cells(r, 3) & "\Cash.pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
ActiveWindow.Close SaveChanges:=False
r = r + 1
Loop
End Sub
Cells, when unqualified, refers to the active sheet. You change the active sheet, so it isn't referring to what you intended.
Sub Cash_PDF_()
Dim ws As Worksheet
Dim wb As Workbook
Dim r As Long
Set ws = ActiveSheet
r = 2
Do While ws.Cells(r, 5) <> ""
Set wb = Workbooks.Open(FileName:="H:\Investment\Fund Folders\" & ws.Cells(r, 3) & "\" & ws.Cells(r, 5), _
ReadOnly:=True, UpdateLinks:=0)
wb.Sheets("Cash").ExportAsFixedFormat _
Type:=xlTypePDF, _
FileName:="C:\Users\Desktop\Test\" & ws.Cells(r, 3) & "\Cash.pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
wb.Close SaveChanges:=False
r = r + 1
Loop
End Sub

Conditional Statement within Loop

What am i doing wrong? I want to create pdf files of a tab that runs through a list in column A from a different tab. Meanwhile, I want it to refer to the value in column CH to see if it is greater than 0 and if it is so save in a specific folder. If not, save in another folder.
Help is greatly appreciated as I can't find a solution and have been stuck for hours. Thanks!
Code:
Sub Generate_PDF_Files()
Application.ScreenUpdating = False
Sheets("Table").Activate
Range("A7").Activate
Set r = Range("CH7:CH185")
With ActiveSheet
For Each erange In .Range("CH7:CH185")
If erange.Value > 0 Then
Sheets("Att A").Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"L:\Mike89\Violations\" & X & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End If
If erange.Value = 0 Then
Sheets("Att A").Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"L:\Mike89\No Violations\" & X & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False,OpenAfterPublish:=False
End If
Next
Do Until ActiveCell.Value = "STOP"
X = ActiveCell.Value
Range("DLR_NUM") = "'" & X
Sheets("Table").Activate
ActiveCell.Offset(1, 0).Activate
Loop
End with
End Sub
Sub Generate_PDF_Files()
Dim c As Range, X, fName As String, shtAtt As Worksheet
Set shtAtt = Sheets("Att A")
For Each c In Sheets("Table").Range("CH7:CH185").Cells
X = c.EntireRow.Cells(1).Value
If X = "STOP" Then Exit For
c.Parent.Range("DLR_NUM") = "'" & X
fName = "L:\Mike89\" & IIf(c.Value = 0, "No Violations\", "Violations\") & X & ".pdf"
shtAtt.ExportAsFixedFormat Type:=xlTypePDF, Filename:=fName, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
Next c
End Sub