英文:
Saving multiple graphs as images through vba
问题
早上好,
我有一个包含多个图形的Excel文件,
每个图形都被命名为图形_i,其中i是从1到100的数字。
我想要编写一个VBA代码,使我能够将这些图形保存为图像。
图像的标题必须与Excel中图形的标题相同。
非常感谢!
我是VBA的新手,所以我正在寻找一些条件语句,
但我不确定我在做什么。
英文:
Good morning,
I have an excel file that has multiple graphs
each graph is named graph_i where i is a number from 1 to 100
I would like to write a vba code that enable me to save these graphs as images.
The title of the image must be identical to the tile of the graph in excel
thank you so much
I am new to vba so I was searching for some if statements
but I am not sure what I am doing
答案1
得分: 1
这是您的代码部分的翻译:
这是您要求的,用于检测图表标题中的无效字符以用作文件名。
子程序 保存图表()
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 "图表已保存为图像文件(png)在 " & ThisWorkbook.Path
End Sub
这是一个不检查的简化版本,使用图表的名称作为文件名:
子程序 保存图表()
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 "图表已保存为图像文件(png)在 " & ThisWorkbook.Path
End Sub
这个版本接受一个可选参数,我们可以指定要保存的工作表的名称,如果不提供参数,它将检查所有工作表:
子程序 保存图表(可选 wsName As String = vbNullString)
Dim ws As Worksheet, chrtOb As Object, title, c As Long, p As Long, l As Long, ch As String, cnt As Long
Const notvalid = "<>:/\|?*"
For Each ws In ThisWorkbook.Worksheets
If wsName = vbNullString Or ws.Name = wsName Then
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
l = Len(notvalid)
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"
cnt = cnt + 1
Next
If wsName <> vbNullString Then Exit For
End If
Next
If cnt = 0 Then
MsgBox "未找到要保存的图表!"
Else
MsgBox cnt & "个图表已保存为图像文件(png)在 " & ThisWorkbook.Path
End If
End Sub
英文:
This is that you ask, with testing for non valid caharacters in graph title to use for filename.
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
This is a simpler version without checking... and use as filename the name of the chart.
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
This version accepts an optional parameter, the name of the sheet we want, if we don't give the parameter it checks all sheets
Sub SaveCharts(Optional wsName As String = vbNullString)
Dim ws As Worksheet, chrtOb As Object, title, c As Long, p As Long, l As Long, ch As String, cnt as long
Const notvalid = "<>:""/\|?*"
For Each ws In ThisWorkbook.Worksheets
If wsName = vbNullString Or ws.Name = wsName Then
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
l = Len(notvalid)
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"
cnt = cnt + 1
Next
If wsName <> vbNullString Then Exit For
End If
Next
if cnt = 0 then
MsgBox "No charts found to save!"
else
MsgBox cnt & " Charts Saved As Image File (png) in " & ThisWorkbook.Path
End If
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论