excel 通过VBA将多个图形保存为图像

olhwl3o2  于 2023-06-07  发布在  其他
关注(0)|答案(1)|浏览(413)

早上好
我有一个excel文件,里面有多个图表
每个图被命名为graph_i,其中i是从1到100的数字
我想写一个vba代码,使我能够保存这些图形图像。
图像的标题必须与excel中图形的平铺相同
非常感谢
我是新的vba,所以我正在寻找一些如果声明
但我不知道我在做什么

yr9zkbsy

yr9zkbsy1#

这是你问的,测试图形标题中用于文件名的无效字符。

Sub SaveCharts()
    Dim ws As Worksheet, chrtOb As Object, title, c As Long, p As Long, l As Long, ch As String
    Const notvalid = "<>:""/\|?*"
    l = Len(notvalid)
    For Each ws In ThisWorkbook.Worksheets
        For Each chrtOb In ws.ChartObjects
            title = vbNullString
            On Error Resume Next
            title = chrtOb.Chart.ChartTitle.Caption
            On Error GoTo 0
            If title = vbNullString Then
                title = chrtOb.Name
            Else
                For c = 1 To l
                    ch = Mid$(notvalid, c, 1)
                    p = 1
                    Do
                        p = InStr(p, title, ch)
                        If p <= 0 Then Exit Do
                        Mid$(title, p, 1) = "_"
                    Loop
                Next
            End If
            chrtOb.Chart.Export filename:=ThisWorkbook.Path & "\" & title & ".PNG", Filtername:="PNG"
        Next
    Next
    MsgBox "Charts Saved As Image File (png) in " & ThisWorkbook.Path
End Sub

这是一个简单的版本,没有检查...并使用图表的名称作为文件名。

Sub SaveCharts()
    Dim ws As Worksheet, chrtOb As Object
    
    For Each ws In ThisWorkbook.Worksheets
        For Each chrtOb In ws.ChartObjects
            chrtOb.Chart.Export filename:=ThisWorkbook.Path & "\" & chrtOb.Name & ".PNG", Filtername:="PNG"
        Next
    Next
    MsgBox "Charts Saved As Image File (png) in " & ThisWorkbook.Path
End Sub

相关问题