VBA缺less引用使其他人无法加载

我在Excel中有2个VBAmacros。 macros1从另一个Excel文件复制一些值。 macros2请求来自彭博社的一些价值观。 macros1需要引用“Microsoft Excel 12.0对象库”,而macros2需要添加对“彭博数据types库”的引用。

我希望这样,即使在没有安装Bloomberg Excel API的机器上,用户也可以运行macros1。 但是,当我尝试运行macros1我到这一行:

Set XL = CreateObject("Excel.Application") 

下面,并得到以下错误:

  "Cant Find Project or Library" 

这似乎是因为它无法find“彭博数据types库”的参考,它也没有加载“Microsoft Excel 12.0对象库”。

在安装了Bloomberg Excel API的计算机上,一切都按预期运行。

如果你想在两台机器上使用同一个文件,首先在VBA Editor – > Tools – > References中删除对BBG的引用。 然后在你的代码中做这样的事情:

 On Error Resume Next ' turn off errors Set XL = CreateObject("Excel.Application") Set BBG = CreateObject("BBGName") If Not IsEmpty(BBG) Then ' Do work with BBG End If On Error GoTo 0 ' turn on errors 

这将允许您只有BBG库存在时才使用BBGmacros,如果不存在则不会抛出错误。