excel - 将图表导出为图像 - 只需单击按钮

标签 excel vba

我正在尝试创建一个按钮,将“图表”工作表中的图表导出为 jpeg 文件。这是我的代码,但它不断显示此错误:

runtime error 424: object required

专门为此:

Set myChart = Graphs.ChartObjects(3).Name = "Chart4"

这是代码

Sub ExportChart()
    Dim myChart As Chart
    Dim myFileName As String
    Set myChart = Graphs.ChartObjects(3).Name = "Chart4"
    myFileName = "myChart.jpg"
    On Error Resume Next
    Kill ThisWorkbook.Path & "\" & myFileName
    myChart.Export Filename:=ThisWorkbook.Path & "\" & myFileName, Filtername:="PNG"
    MsgBox "OK"
    Set myChart = Nothing
End Sub

谢谢大家!

最佳答案

这就是你正在尝试的吗?

此外,如果您想将其另存为 jpg 那么为什么要使用 png 过滤器?我已将“myChart.jpg”更改为“myChart.png”。根据需要进行更改。

Sub ExportChart()
    Dim objChrt As ChartObject
    Dim myChart As Chart

    Set objChrt = Sheets("Graphs").ChartObjects(3)
    Set myChart = objChrt.Chart

    myFileName = "myChart.png"

    On Error Resume Next
    Kill ThisWorkbook.Path & "\" & myFileName
    On Error GoTo 0

    myChart.Export Filename:=ThisWorkbook.Path & "\" & myFileName, Filtername:="PNG"

    MsgBox "OK"
End Sub

关于excel - 将图表导出为图像 - 只需单击按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11939087/

相关文章:

excel - 删除图表中的数据系列

regex - Excel VBA 中的正则表达式

excel - VBA 从列表中删除 Excel 列

vba - 如何使用 VBA 分配包含符号的单元格公式?

vba - 从网站提取数据到excel错误

excel - 我可以通过 vlookup 在单元格中找到某个单词吗?

string - VBA 修剪留下前导空白

VBA Excel : Concatenate a range of cells

excel - 如何从 VBA excel 代码中解锁 X SYSTEM PCOMM ibm as400 终端?如果不是 autECLOIA.InputInhibited = 0 那么

excel - 如何使用Excel VBA round()进行四舍五入?