我有一个xlsb格式的excel工作簿(我们称其为“子工作簿”),该工作簿是由另一个excel工作簿(“父工作簿”)自动生成的。 所谓的“儿童工作簿”有一些工作表,其中包含图表。我希望这些工作表具有每次用户选择工作表时都运行的代码,并且我希望“父母工作簿”将该代码传输到“子工作簿”。转移已成功完成,该模块确实会出现在Child工作簿的项目中,直到我关闭它为止(当然,我会在此之前保存它...)。
Here is an image of how it looks like after saving but before closing "Child.xlsb".
我的问题是,当我重新打开Child工作簿时,该模块仍然存在,但是其内容完全消失了,它是空的。
Here is an image of how it looks like after saving, closing and reopening it.
有什么建议吗?
Sub TransferModule()
Const MODULE_NAME As String = "DiagramMakro"
Dim MODULEFILE As String
MODULEFILE = "C:\Modul.txt"
Workbooks("Parent.xlsm").VBProject.VBComponents(MODULE_NAME).Export MODULEFILE
For i = 1 To Workbooks("CHILD.xlsb").Sheets.Count
If Workbooks("CHILD.xlsb").Worksheets(i).Name Like "Diagram*" Then
Workbooks("CHILD.xlsb").Worksheets(i).Activate
Workbooks("CHILD.xlsb").VBProject.VBComponents(ActiveSheet.CodeName).Name = MODULE_NAME
Workbooks("CHILD.xlsb").VBProject.VBComponents.Item("CHILD.xlsb").CodeModule.AddFromFile (MODULEFILE)
End If
Next i
Kill MODULEFILE
End Sub
这是要转移的模块:
Private Sub Worksheet_Activate()
Dim myChart As Chart
Dim mySeries As SeriesCollection
Set myChart = ActiveSheet.ChartObjects(1).Chart
Set mySeries = myChart.SeriesCollection
For i = 3 To mySeries.Count
mySeries.Item(i).Format.Line.Visible = msoFalse
Next i
End Sub