A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
Hi,
scenario:
export chart from the active sheet to pdf , paper A4
vba code
Sub ActiveChart_to_PDF_A4()
'## 06-Feb-2023 ##
Dim ws As Worksheet
Set ws = ActiveSheet
Dim sPath As String
sPath = ThisWorkbook.Path & ""
Dim obj As ChartObject
Dim sh As Shape
Dim objName As String, sAdr As String, s1 As String, s2 As String
Set obj = ws.ChartObjects(1)
objName = obj.Name
Application.ScreenUpdating = False
Sheets.Add
With ActiveSheet.Cells
.RowHeight = 2
.ColumnWidth = 0.2
End With
obj.CopyPicture
ActiveSheet.Paste
Set ws = ActiveSheet
Set sh = ws.Shapes(1)
sh.Width = Application.InchesToPoints(11.6)
sh.Height = Application.InchesToPoints(8)
s1 = sh.TopLeftCell.Address
s2 = sh.BottomRightCell.Address
sAdr = s1 & ":" & s2
With ws.PageSetup
.PrintArea = sAdr
.LeftMargin = Application.InchesToPoints(0)
.RightMargin = Application.InchesToPoints(0)
.TopMargin = Application.InchesToPoints(0)
.BottomMargin = Application.InchesToPoints(0)
.HeaderMargin = Application.InchesToPoints(0)
.FooterMargin = Application.InchesToPoints(0)
.PaperSize = xlPaperA4
.Zoom = False
.FitToPagesTall = 1
.FitToPagesWide = 1 '
.CenterHorizontally = True
.CenterVertically = True
.Orientation = xlLandscape
End With
ws.ExportAsFixedFormat xlTypePDF, Filename:=sPath & Format(Date, "yyyymmdd-") & objName & ".pdf", OpenAfterPublish:=True
Application.DisplayAlerts = False
ws.Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
==========================
step1
Save your Workbook with extension .xlsm (macros enabled workbook)
Step2
2a) press ALT+F11 to open Visual Basic
2b) from the ribbon, select: Insert > Module and paste the code above on the right
2c) Press ALT+Q to Close Visual Basic
Step3
To run the macro, press ALT+F8,
select '**ActiveChart_to_PDF_A4'**from the list and click the run button.
or
add a button and assign the vba macro