将图表导出为图像 - 点击一个按钮 [英] Export chart as image - with click of a button

查看:124
本文介绍了将图表导出为图像 - 点击一个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个按钮,将图表以Graphs的形式导出为jpeg文件。这是我的代码,但是它会继续显示此错误:

I'm trying to create a button that would export a chart in sheet "Graphs" as a jpeg file. This is the code I have, however it keeps on showing this error:


运行时错误424:需要对象

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。

Also if you are trying to save it as jpg then why a png filter? I have changed "myChart.jpg" to "myChart.png". Change as applicable.

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

这篇关于将图表导出为图像 - 点击一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆