Exporting As PDF opens only second chart - excel

Trying to export my "Report" worksheet as a PDF and open it. Issue is that all that's opening up is the second chart at the bottom of that worksheet.
My code:
ThisWorkbook.Sheets("Report").Activate
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=ActiveWorkbook.Path & "\Report.pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
What's supposed to be showing in the PDF:
What's actually showing up in the PDF:

It is a well known problem of Excel: For it ActiveSheet can be a Chart, too...
If you have an active chart, this one will be exported.
So, it is enough to place before the exporting line :
ActiveSheet.Range("A1").Select
Or be sure that a chart is not selected and even better, try qualifying your sheet as:
Dim sh As Worksheet
Set sh = Worksheets("sheet_name")

Try the following code before exporting:
With ActiveSheet.PageSetup
.Orientation = xlLandscape
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With

Related

Fill set columns to pdf

I am exporting a .xlsm sheet to PDF using Visual Basic (VBA).
I only want to export the columns A:Z.
I have tried this:
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
With ws.PageSetup
.PrintArea = "$A:$Z"
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
.Orientation = xlLandscape
.PaperSize = xlPaperA3
.RightMargin = Application.InchesToPoints(0.25)
End With
Next
ThisWorkbook.Sheets("Diagram").Select
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=strFilename, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=True, _
OpenAfterPublish:=False
But I get space on the right side like it has filled the whole page just hid the columns I didn't select...
Is it any way to fix this problem and make column A:Z fill the whole page?
Any help is greatly appreciated :)

Export the same range changed by a function to a PDF file

I have only found functions to export several sheets in a single PDF.
I want to export the same sheet and the same range, which is changing through a function, in a single PDF file.
For example, in the following code there is a loop that changes the values of a range three times.
How can I save a single PDF file that contains the range in triplicate and changed?
I tried arrays but I don't know how to implement in this specific case.
Dim i As Long
For i = 1 To 3
Call function_to_change_values 'this function change the values of range A1 to D5
'config printing:
ActiveSheet.PageSetup.PrintArea = "$A$1:$D$5" 'print area
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.Orientation = xlLandscape
.Zoom = 77
.Draft = False
.PaperSize = xlPaperA4
End With
'create PDF:
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="C:\myfile.pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Next i

How do I fit my data to one sheet when exporting to pdf in vba?

I am trying to export a specific range on one of my excel sheets to pdf. It cuts off a small part at the bottom and puts it on the next page. My question is : How do I change my code below to allow for a "fit to one page" option and how do I make the orientation landscape?
Sub printdispatchsheet()
Sheets("DispatchSheet").Range("A1:J48").ExportAsFixedFormat
Type:=xlTypePDF, fileName:= _
"c:\Users\name\Desktop\DispatchSheet.pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=True, _
OpenAfterPublish:=True
End Sub
Could you try and see if this works? Also see if setting your print areas properly helps, if not modify that part of the code to True
Sub printdispatchsheet()
With Sheets("DispatchSheet")
.PageSetup.Orientation = xlLandscape
.PageSetup.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
Sheets("DispatchSheet").Range("A1:J48").ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="c:\Users\name\Desktop\DispatchSheet.pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End Sub

Save Excel sheet to pdf

I have a macro that would save a sheet to pdf format but it doesn't work anymore.
With ActiveSheet.PageSetup
.CenterHeader = strFile2
.Orientation = xlPortrait
.PrintArea = "a1:q21"
'.PrintTitleRows = ActiveSheet.Rows(5).Address
'.Zoom = False
.FitToPagesTall = False
.FitToPagesWide = 1
End With
ws.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=strFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
I tried
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=OpenPDFAfterCreating
It gives the same
invalid procedure call or argument.
The macro works at my colleague's PC.
Have you declared variables outside of the snippet of code you pasted? You need to declare ws as a worksheet or you are going to have issues:
Dim ws as Worksheet
You need to set ws = ActiveSheet in order to use it, though I would recommend avoiding ActiveSheet since it can lead to issues. Something better would be:
Set ws = Workbooks("**Name of your workbook**").Worksheets("**Name of worksheet**")
With ws.PageSetup
**code**
End With
Hope that helps.

Dynamically Change File Name - ComboBox Excel Print

How can I have the name of the pdf dynamically changed based on Sel_Manager (combobox1) ? The viewers would like to print multiple PDFs and show them on the screen at the same time. Thank you in advance!
Private Sub CommandButton1_Click()
Dim Sel_Manager As String
'Specify headers to be repeated at the top
With ActiveSheet.PageSetup
.PrintTitleRows = "$5:$9"
.PrintTitleColumns = "$B:$M"
.Orientation = xlLandscape
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
'Manager selection through simple Inputbox
Sel_Manager = ComboBox1
'Insert autofilter for worksheet
Cells.Select
Selection.AutoFilter
'Select manager defined in inputbox
ActiveSheet.Range("B14", Range("M14").End(xlDown)).AutoFilter Field:=1, Criteria1:=Sel_Manager
'Select range to be printed and specify manager in filename
ActiveSheet.Range("B14", Range("M14").End(xlDown)).Select
Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"Manager.pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
ActiveSheet.ShowAllData
End Sub
Answer found -
Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
Sel_Manager + ".pdf",

Resources