我尝试使用VBA将多个图表导出为单独的pdf文件,但我想知道每个pdf文件的文件名是否可能不同,并从一个范围内的单元格值中获取(Ex .A2-A6)使文件的名称基于相应的单元格(A2),下一个文件的名称将基于它下面的单元格(A3),依此类推。
这是我到目前为止所拥有的。基本上这些名称是唯一的,但我只限于数字(i = 1)的变化。
Sub Macro9()
Sheets("Charts").Select
i = 1
For Each myChart In ActiveSheet.ChartObjects
myChart.Activate
myPDF = "\\stchsfs\arboari$\Profile-Data\Desktop\Export Trial1\Graph Export_" & i & ".pdf"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=myPDF, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
i = i + 1
Next myChart
End Sub
谢谢!
答案 0 :(得分:2)
尝试下面的代码,代码注释中的解释:
Option Explicit
Sub Macro9()
Dim myChart As ChartObject
Dim myPDF As String
Dim i As Long
With Sheets("Charts") '<-- use With instead of using Select
i = 1
For Each myChart In .ChartObjects
myChart.Activate
myPDF = "\\stchsfs\arboari$\Profile-Data\Desktop\Export Trial1\Graph Export_" & Sheets("Master Sheet").Range("A" & i + 1).Value2 & ".pdf" '<-- modifed this line
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=myPDF, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
i = i + 1
Next myChart
End With
End Sub