打开另一个工作簿后,如何自动更新Vlookup链接?

时间:2019-12-02 18:59:00

标签: excel vba vlookup auto-update

我有两个工作簿,分别是“ Test1” “ Test2” 。该代码在“ Test1” 工作簿中,在 “ ThisWorkbook” 模块中。

我在“ Test1” 工作簿内的“ Mytest1” 工作表中有一个Vlookup,它只是对工作簿“ Test2” 做了一个Vlookup >内页“ Mytest2” 。这两个工作簿均受密码保护,如果我在另一台计算机上打开工作簿“ Test2” ,会提示我输入“ Test2”的提示“输入密码” 打开我的“ Test1” 工作簿时,点击strong>。我需要使Vlookups自动更新,而无需任何提示输入密码或打开第二个工作簿的提示。

我在下面同时贴了工作簿和代码的屏幕截图,以及代码本身。请编辑或询问我任何问题以澄清任何内容。

Private Sub Workbook_Open()

          Workbooks.Open Filename:="\\FILEPATH\Databases\Test 2.xlsm", Password:="Swarf", Updatelinks:=3
          ActiveWorkbook.Close SaveChanges:=True

End Sub

Code inside the "Test1" workbook

"Test1" workbook "Test2" workbook

2 个答案:

答案 0 :(得分:0)

我没有时间测试,但是稍后。我会尝试像这样将VLOOKUP重写为VLOOKUP的包装器

Function VLOOKUP_BYPASS(LookFor As Variant, Rng As Excel.Range, lngCol As Long) As Variant

Dim strWorkbook As String

strWorkbook = Rng.Parent.Parent.Name

If WORKBOOKOPEN(strWorkbook) Then
    VLOOKUP_BYPASS = Application.WorksheetFunction.VLookup(LookFor, Rng, lngCol, False)
Else
    VLOOKUP_BYPASS = "NA"
End If

End Function

Function WORKBOOKOPEN(strWorkbookName As String) As Boolean

WORKBOOKOPEN = False

Dim wb As Excel.Workbook

On Error GoTo eHandle

Set wb = Workbooks(strWorkbookName)
WORKBOOKOPEN = True

Exit Function

eHandle:


End Function

答案 1 :(得分:0)

这是我想出的解决方案,我只是将用来引用我的Vlookup(“测试2” 工作簿”)的文件打开为只读文件!

Private Sub Workbook_Open()

Workbooks.Open Filename:="\\FILEPATH \Databases\Test 2.xlsm", Password:="Swarf", Updatelinks:=3,ReadOnly:=True

ActiveWorkbook.Close SaveChanges:=False

End Sub