编程不是我的主要工作功能,但似乎是我所认为的瑞士军刀,我的任务是在Excel中制作一个VBA宏,将图形导出为gif文件,以便自动更新信息屏幕我们的制造工厂。
我有一个可行的宏,但是,它有时会失败并创建一个带有正确文件名但是“空”图形的gif。
用户在工作表的范围内定义自己的导出路径以及导出的图表的尺寸。
Sub ExportAllCharts()
Application.ScreenUpdating = False
Const sSlash$ = "\"
Const sPicType$ = "gif"
Dim sChartName As String
Dim sPath As String
Dim sExportFile As String
Dim ws As Worksheet
Dim wb As Workbook
Dim chrt As ChartObject
Dim StdXAxis As Double
Dim StdYAxis As Double
Dim ActXAxis As Double
Dim ActYAxis As Double
Dim SheetShowPct As Double
Set wb = ActiveWorkbook
Set ws = ActiveSheet
StdXAxis = Range("StdXAxis").Value
StdYAxis = Range("StdYAxis").Value
sPath = Range("ExportPath").Value
If sPath = "" Then sPath = ActiveWorkbook.Path
For Each ws In wb.Worksheets 'check all worksheets in the workbook
If ws.Name = "Graphs for Export" Then
SheetShowPct = ws.Application.ActiveWindow.Zoom
For Each chrt In ws.ChartObjects 'check all charts in the current worksheet
ActXAxis = chrt.Width
ActYAxis = chrt.Height
With chrt
If StdXAxis > 0 Then .Width = StdXAxis
If StdYAxis > 0 Then .Height = StdYAxis
End With
sChartName = chrt.Name
sExportFile = sPath & sSlash & sChartName & "." & sPicType
On Error GoTo SaveError:
chrt.Chart.Export Filename:=sExportFile, FilterName:=sPicType
On Error GoTo 0
With chrt
.Width = ActXAxis
.Height = ActYAxis
End With
Next chrt
ws.Application.ActiveWindow.Zoom = SheetShowPct
End If
Next ws
Application.ScreenUpdating = True
MsgBox ("Export Complete")
GoTo EndSub:
SaveError:
MsgBox ("Check access rights for saving at this location: " & sPath & Chr(10) & Chr(13) & "Macro Terminating")
EndSub:
End Sub
收到帮助后,这是我最终放入工作簿的宏代码: 谢谢你的帮助。
Const sPicType$ = "gif"
Sub ExportAllCharts()
Application.ScreenUpdating = False
Dim sChartName As String, sPath As String, sExportFile As String
Dim ws As Worksheet
Dim wb As Workbook
Dim chrt As ChartObject
Dim StdXAxis As Double, StdYAxis As Double, ActXAxis As Double
Dim ActYAxis As Double, SheetShowPct As Double
Set wb = ActiveWorkbook
StdXAxis = Range("StdXAxis").Value
StdYAxis = Range("StdYAxis").Value
sPath = Range("ExportPath").Value
If sPath = "" Then sPath = ActiveWorkbook.Path
Set ws = wb.Sheets("Graphs for Export")
For Each chrt In ws.ChartObjects
With chrt
ActXAxis = .Width
ActYAxis = .Height
If StdXAxis > 0 Then .Width = StdXAxis
If StdYAxis > 0 Then .Height = StdYAxis
sExportFile = sPath & "\" & .Name & "." & sPicType
.Select
.Chart.Export Filename:=sExportFile, FilterName:=sPicType
.Width = ActXAxis
.Height = ActYAxis
End With
Next chrt
Application.ScreenUpdating = True
MsgBox ("Export Complete")
End Sub
答案 0 :(得分:4)
两件事
1)删除“On Error Resume Next”。你怎么知道路径是否正确?
2)为什么不循环浏览图表对象而不是循环遍历形状?例如
Dim chrt As ChartObject
For Each chrt In Sheet1.ChartObjects
Debug.Print chrt.Name
chrt.Chart.Export Filename:=sExportFile, FilterName:=sPicType
Next
<强>后续强>
试试这个。
Const sPicType$ = "gif"
Sub ExportAllCharts()
Application.ScreenUpdating = False
Dim sChartName As String, sPath As String, sExportFile As String
Dim ws As Worksheet
Dim wb As Workbook
Dim chrt As ChartObject
Dim StdXAxis As Double, StdYAxis As Double, ActXAxis As Double
Dim ActYAxis As Double, SheetShowPct As Double
Set wb = ActiveWorkbook
StdXAxis = Range("StdXAxis").Value
StdYAxis = Range("StdYAxis").Value
sPath = Range("ExportPath").Value
If sPath = "" Then sPath = ActiveWorkbook.Path
Set ws = wb.Sheets("Graphs for Export")
For Each chrt In ws.ChartObjects
ActXAxis = chrt.Width
ActYAxis = chrt.Height
With chrt
If StdXAxis > 0 Then .Width = StdXAxis
If StdYAxis > 0 Then .Height = StdYAxis
sChartName = .Name
sExportFile = sPath & "\" & sChartName & "." & sPicType
.Select
.Chart.Export Filename:=sExportFile, FilterName:=sPicType
.Width = ActXAxis
.Height = ActYAxis
End With
Next chrt
MsgBox ("Export Complete")
Exit Sub
SaveError:
MsgBox ("Check access rights for saving at this location: " & sPath & _
Chr(10) & Chr(13) & "Macro Terminating")
End Sub
答案 1 :(得分:1)
我只是想出了零图表思考的问题。我听说有人说excel有一个bug,但实际上并没有。以某种方式excel拍摄快照或类似图形的东西,然后导出图像,您可以使用您想要的任何扩展。您必须确保的是,您向右滚动到工作表的顶部,并确保您要导出的所有图形都是可变的(对您而言)。如果任何图形在下面,那么即使你参考它也不会导出,所以你需要将它一直拖到顶部直到你可以看到它。只要确保你看到细胞(A1)。它有效!!!