我有一个宏,我在网上找到,并已用于允许我导出活动工作簿内的所有图表对象。当我拥有它自己的正常工作簿时,它似乎工作得很好。VB个人工作簿错误,其中宏运行时,我每次打开Excel
但是,我希望这是一个通用函数,可以在任何给定的工作簿上使用,因此我将这些代码放入了我个人的Excel工作簿中。做完这些之后,我注意到现在的代码每次打开我的Excel应用程序时都会运行,而且我基本上都会看到大量打开空的“.png”文件的窗口。
如何防止每次打开应用程序时运行此代码?它是一个优秀的设置或宏代码固有的错误?
我在我的个人工作簿中有其他的宏,看起来正常工作(只有在通过工具> VB>宏菜单选择时才运行),所以我觉得代码有问题。任何帮助将不胜感激。
'the main problematic function'
Sub ExportAllPossibleCharts()
Dim i As Integer, exportCount As Integer
Dim fileNum As String, fileBase As String
Dim sheetObj As Worksheet
Dim chartObj As Chart
fileBase = ActiveWorkbook.FullName
fileBase = Replace(fileBase, ".xlsx", "")
exportCount = 0
'First, export all charts that are in their own sheets'
For Each chartObj In ActiveWorkbook.Charts
fileNum = NiceFileNumber(exportCount)
exportCount = exportCount + 1
'Do the export'
chartObj.Export fileBase & "_chart" & fileNum & ".png"
Next
'Then, export all charts that are embedded inside normal sheets'
For Each sheetObj In ActiveWorkbook.Worksheets
For i = 1 To sheetObj.ChartObjects.Count
fileNum = NiceFileNumber(exportCount)
exportCount = exportCount + 1
'Do the export'
sheetObj.ChartObjects(i).Activate
ActiveChart.Export fileBase & "_chart" & fileNum & ".png"
Next i
Next
End Sub
'small nicety to ensure two-digits for better file sorting'
Function NiceFileNumber(num As Integer) As String
If num < 10 Then
NiceFileNumber = "0" & num
Else
NiceFileNumber = num
End If
End Function
其中VBA项目(我的意思是哪个对象模块)放置了这个代码?你能找到来自Thi的Workbook_Open事件吗? sWorkbook'对象模块并向我们提供代码? – 2013-12-11 14:05:44
该代码目前生活在Module 1中。我将如何找到这些事件来向您发送代码? – user3091348
好吧我找到了“ThisWorkbook对象”,目前这个对象中没有代码,我可以看到 – user3091348