简体   繁体   中英

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. 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

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.

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. 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.

I tried:

ThisWorkbook.UpdateLink

But all I get is runtime error 1004: Method 'UpdateLink' of object '_Workbook' failed.

This is a really easy one to fix!

The code you wrote starts a new instance of 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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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