[英]Excel isn't saving VBA reference
I created an Excel .xlsm file with a bunch of functionality in some VBA Modules/Classes and now I've decided to separate out the code because it will be shared across 4 different sites. 我在一些VBA模块/类中创建了一个带有大量功能的Excel .xlsm文件,现在我决定将代码分开,因为它将在4个不同的站点共享。
I saved the vba_code.xlsm to a shared location and created my 4 different siteXYZ.xlsm files. 我将vba_code.xlsm保存到共享位置并创建了我的4个不同的siteXYZ.xlsm文件。
In each siteXYZ.xlsm file I would go to the "Tools | References" menu in the VBA editor and add a reference to the shared vba_code.xlsm at a shared file location \\share_location\\fileLocation\\vba_code.xlsm 在每个siteXYZ.xlsm文件中,我将转到VBA编辑器中的“工具|引用”菜单,并在共享文件位置\\ share_location \\ fileLocation \\ vba_code.xlsm中添加对共享vba_code.xlsm的引用。
At this point, I tested out the functions in siteXYZ.xlsm and everything would work fine. 此时,我测试了siteXYZ.xlsm中的函数,一切都运行正常。
However, every time I saved siteXYZ.xlsm, closed Excel and then reopened the file it would lose the reference to my vba_code.xlsm file. 但是,每次我保存siteXYZ.xlsm,关闭Excel然后重新打开文件,它将丢失对我的vba_code.xlsm文件的引用。
How can I keep the VBA references saved with my siteXYZ.xlsm file? 如何使用siteXYZ.xlsm文件保存VBA引用?
After spending hours searching for an answer and trying various methods such as adding a digital signature to the vba_code.xlsm file and trying to programmatically add the reference on Workbook_open I found a forum post describing the problem: 花了几个小时搜索答案并尝试各种方法,例如向vba_code.xlsm文件添加数字签名,并尝试以编程方式在Workbook_open上添加引用,我发现了一个描述问题的论坛帖子:
My siteXYZ.xlsm file had no VBA code or macros defined within it so Excel refused to save the VBA Project and as a result did not save the VBA Reference to vba_code.xlsm. 我的siteXYZ.xlsm文件中没有定义VBA代码或宏,因此Excel拒绝保存VBA项目,因此未将VBA引用保存到vba_code.xlsm。
The solution was simple: 解决方案很简单:
Add ANY VBA code to the siteXYZ.xlsm and save it. 将任何 VBA代码添加到siteXYZ.xlsm并保存。
I just double-clicked ThisWorkbook under the VBA editor and added a function to Workbook_open that doesn't do anything: 我只是在VBA编辑器下双击ThisWorkbook,并向Workbook_open添加了一个不执行任何操作的函数:
Private Sub Workbook_open()
End Sub
The usual method of achieveing this is by saving your vba_code.xlsm as an addin (XLA or XLAM) and storing it in the shared location, then adding the addin to Excel in your 4 different sites. 实现此目的的通常方法是将vba_code.xlsm保存为插件(XLA或XLAM)并将其存储在共享位置,然后将插件添加到4个不同站点的Excel中。
You can also extend this approach by using your own Addin Loader instead of Excel's. 您也可以使用自己的Addin Loader而不是Excel来扩展此方法。
There is a working example of an Addin Loader at http://www.decisionmodels.com/downloads.htm 在http://www.decisionmodels.com/downloads.htm上有一个Addin Loader的工作示例
Following @nvuono 关注@nvuono
You need to add some kind of module/reference to the excel file for it to save the references you have added. 您需要为excel文件添加某种模块/引用,以便保存您添加的引用。
Private Function addJunkModuleToGetReferencesToSave(ByRef wb As Workbook)
Set new_module = wb.VBProject.VBComponents.Add(vbext_ct_ClassModule)
new_module.name = "Junk"
new_module.CodeModule.AddFromString ""
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.