简体   繁体   English

Excel-链接未使用VBA更新

[英]Excel - Links Not Updating with VBA

I have searched far and wide for an answer to this issue, but nothing I try seems to work. 我一直在寻找有关此问题的答案,但我尝试的任何方法似乎都无效。 Here is where I've got to so far. 到目前为止,这是我必须到达的地方。

I have two Excel workbooks. 我有两个Excel工作簿。 The first workbook is a workbook that people can edit. 第一个工作簿是人们可以编辑的工作簿。 It contains things such as drop-down menus which are only functional when linked to a source file. 它包含诸如下拉菜单之类的内容,这些菜单仅在链接到源文件时才起作用。 The second workbook is the source file. 第二个工作簿是源文件。 It contains information such as tables of data which are displayed in the drop-down menu lists in the first workbook. 它包含诸如数据表之类的信息,这些信息显示在第一个工作簿的下拉菜单列表中。 (Eg A list of medical conditions, like asthma, diabetes, etc. and people can select a condition from the list.) (例如,医疗状况清单,例如哮喘,糖尿病等,人们可以从清单中选择一种状况。)

The problem I'm having at the moment is that these links no longer update properly. 我目前遇到的问题是这些链接不再正确更新。

To clarify, they will update if I open the source file and then the output file manually. 为了澄清,如果我先打开源文件然后手动打开输出文件,它们将更新。 In the name manager, the references will appear like this: 在名称管理器中,引用将如下所示:

SourceFile!NameRef1 的SourceFile!NameRef1

Because I didn't want people to have to open the files one at a time manually, I wrote some VBA which opens the source file automatically whenever you open the output file. 因为我不希望人们必须一次手动打开一个文件,所以我写了一些VBA,该VBA在您打开输出文件时自动打开源文件。

Dim app As New Excel.Application
Dim book As Excel.Workbook
Private Sub Workbook_Open()
app.Visible = True
Set book = app.Workbooks.Open("C:\Users\user_000\Desktop\ExampleFolder\SourceFile.xlsm")
End Sub

It works fine, but the links will not update, despite the fact that they are set to update automatically. 它可以正常工作,但是即使将链接设置为自动更新,它们也不会更新。 At this point, when I go into the name manager, the links have now changed to the entire file path. 此时,当我进入名称管理器时,链接现在已更改为整个文件路径。 For some reason, every time it has done this (for example, when I've moved the file to another location), it stops updating the links. 由于某种原因,每次执行此操作时(例如,当我将文件移到另一个位置时),它将停止更新链接。

I don't understand why Excel doesn't like you using file paths as links in the name manager. 我不明白为什么Excel不喜欢您在名称管理器中使用文件路径作为链接。 It always works when I refer to the file name only. 仅在引用文件名时,它始终有效。 I've tried everything I can think of in VBA, I've changed the macro security settings to enable just about everything, but it makes no difference. 我已经尝试了在VBA中可以想到的所有功能,更改了宏安全性设置以启用几乎所有功能,但这没有什么区别。

I tried: 我试过了:

ThisWorkbook.UpdateLink

But all I get is runtime error 1004: Method 'UpdateLink' of object '_Workbook' failed. 但是我得到的只是运行时错误1004:对象“ _Workbook”的方法“ UpdateLink”失败。

This is a really easy one to fix! 这真的很容易解决!

The code you wrote starts a new instance of Excel 您编写的代码将启动一个新的Excel实例

Dim app As New Excel.Application

It is in this instance you are opening in the source workbook with your data. 在这种情况下,您正在使用数据在源工作簿中打开。 The excel combo boxes in the output file will not be able to pick up the data from this output workbook unless it is open in the SAME instance of excel! 除非在excel的相同实例中打开了输出文件中的excel组合框,否则它将无法从该输出工作簿中获取数据!

So simply change you code to something like this: 因此,只需将您的代码更改为如下所示:

Private Sub Workbook_Open()
Set book =     Workbooks.Open("C:\Users\user_000\Desktop\ExampleFolder\SourceFile.xlsm")
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM