简体   繁体   English

链接到基于另一个单元格的外部工作簿

[英]Link to external workbook based on another cell

I'm working on dynamic external workbook data referencing in Excel, can someone please help considering the following: 我正在使用Excel中的动态外部工作簿数据进行参考,有人可以帮助考虑以下问题:

In my workbook in A2 I have = 'C:\\'&A1&' Reports\\[1.xls]Sheet1'!C1) 在我的A2工作簿中,我= = 'C:\\'&A1&' Reports\\[1.xls]Sheet1'!C1)

A1 will contain either A1将包含

Folder A 文件夹A
Folder B 文件夹B

So depending on the value in A1, I want to point to either 因此,根据A1中的值,我想指出其中一个

D:\\Folder A Reports\\1.xls D:\\ Folder A Reports \\ 1.xls
D:\\Folder B Reports\\1.xls D:\\ Folder B Reports \\ 1.xls

How can I achieve this? 我该如何实现?

Thank you! 谢谢!

Indirect is the function you need: 间接是您需要的功能:

=INDIRECT("'C:\" & A1 & " Reports\[1.xls]Sheet1'!C1")

The problem I see though is it won't resolve if the sheet is not open. 我看到的问题是,如果未打开工作表,它将无法解决。

As INDIRECT doesn't work on closed books and it is expensive to have to open them then the normal soutions for this are to 由于INDIRECT在已关闭的书上不起作用,因此必须打开书本非常昂贵,因此通常的做法是

  1. create a dirty link with VBA. 用VBA创建一个链接。
  2. Use Indirect.Ext from the Morefunc addin. 使用Morefunc插件中的Indirect.Ext
  3. My preferred approach, use XLM which Harlan Grove has improved in his pull function below. 我的首选方法是使用XLM ,Harlan Grove在下面的拉取功能中对其进行了改进。

To use for your purpose: 要用于您的目的:

=pull("'C:\\"&A1&"\\"&"[1.xls]Sheet1'!C1")

From https://numbermonger.wordpress.com/2012/02/11/excel-pull-function-creating-dynamic-links-to-closed-workbooks/ 来自https://numbermonger.wordpress.com/2012/02/11/excel-pull-function-creating-dynamic-links-to-closed-workbooks/

Pull function 拉动功能

Function pull(xref As String) As Variant
'inspired by Bob Phillips and Laurent Longre
'but written by Harlan Grove
'-----------------------------------------------------------------
'Copyright (c) 2003 Harlan Grove.
'
'This code is free software; you can redistribute it and/or modify
'it under the terms of the GNU General Public License as published
'by the Free Software Foundation; either version 2 of the License,
'or (at your option) any later version.
'-----------------------------------------------------------------
'2004-05-30
'still more fixes, this time to address apparent differences between
'XL8/97 and later versions. Specifically, fixed the InStrRev call,
'which is fubar in later versions and was using my own hacked version
'under XL8/97 which was using the wrong argument syntax. Also either
'XL8/97 didn't choke on CStr(pull) called when pull referred to an
'array while later versions do, or I never tested the 2004-03-25 fix
'against multiple cell references.
'-----------------------------------------------------------------

'2004-05-28
'fixed the previous fix - replaced all instances of 'expr' with 'xref'
'also now checking for initial single quote in xref, and if found
'advancing past it to get the full pathname [dumb, really dumb!]
'-----------------------------------------------------------------
'2004-03-25
'revised to check if filename in xref exists - if it does, proceed;
'otherwise, return a #REF! error immediately - this avoids Excel
'displaying dialogs when the referenced file doesn't exist
'-----------------------------------------------------------------
Dim xlapp As Object, xlwb As Workbook
Dim b As String, r As Range, C As Range, n As Long
'** begin 2004-05-30 changes **

'** begin 2004-05-28 changes **
'** begin 2004-03-25 changes **
n = InStrRev(xref, "\")
If n > 0 Then
If Mid(xref, n, 2) = "\[" Then
b = Left(xref, n)
n = InStr(n + 2, xref, "]") - n - 2
If n > 0 Then b = b & Mid(xref, Len(b) + 2, n)
Else
n = InStrRev(Len(xref), xref, "!")
If n > 0 Then b = Left(xref, n - 1)
End If

'** key 2004-05-28 addition **
If Left(b, 1) = "'" Then b = Mid(b, 2)
On Error Resume Next
If n > 0 Then If Dir(b) = "" Then n = 0
Err.Clear
On Error GoTo 0
End If

If n <= 0 Then
pull = CVErr(xlErrRef)
Exit Function
End If
'** end 2004-03-25 changes **
'** end 2004-05-28 changes **
pull = Evaluate(xref)

'** key 2004-05-30 addition **
If IsArray(pull) Then Exit Function
'** end 2004-05-30 changes **

If CStr(pull) = CStr(CVErr(xlErrRef)) Then
On Error GoTo CleanUp 'immediate clean-up at this point

Set xlapp = CreateObject("Excel.Application")
Set xlwb = xlapp.Workbooks.Add 'needed by .ExecuteExcel4Macro

On Error Resume Next 'now clean-up can wait

n = InStr(InStr(1, xref, "]") + 1, xref, "!")
b = Mid(xref, 1, n)

Set r = xlwb.Sheets(1).Range(Mid(xref, n + 1))

If r Is Nothing Then
pull = xlapp.ExecuteExcel4Macro(xref)

Else
For Each C In r
C.Value = xlapp.ExecuteExcel4Macro(b & C.Address(1, 1, xlR1C1))
Next C

pull = r.Value

End If

CleanUp:
If Not xlwb Is Nothing Then xlwb.Close 0
If Not xlapp Is Nothing Then xlapp.Quit
Set xlapp = Nothing

End If

End Function

External references to closed files in Excel formulas can't be dynamic, so maybe something like this: Excel公式中对已关闭文件的外部引用不能是动态的,因此可能是这样的:

=IF(A1="Folder A", 'C:\Folder A Reports\[1.xls]Sheet1'!C1,
                   'C:\Folder B Reports\[1.xls]Sheet1'!C1)

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

相关问题 链接到另一个工作簿的单元格值未更新 - Cell value with link to another workbook not updating 根据另一个工作簿中的单元格值运行一个过程 - Running a procedure based on cell values in another workbook 基于另一个工作簿中的单元格值自动筛选 - Autofilter based on a cell values in another workbook 根据标题从外部工作簿复制数据,而不是为了另一个工作簿 - Copying Data from External workbook based on headers not in order to another workbook 当前工作簿中基于单元格(日期)的Excel外部参考 - Excel External Reference Based on Cell (Date) in Current Workbook VBA进行故障排除,根据两个条件将一个工作簿中的一个单元格复制到另一个工作簿中的另一个单元格 - VBA troubleshoot, copy one cell in one workbook to another cell in another workbook based on two criteria 通过到另一个工作簿中的表的外部引用(链接)更新数据 - Update data by an external reference (link) to a table in another workbook 根据单元格值自动过滤(或循环)并复制到另一个工作簿 - Autofilter (or loop) and copy to another workbook based on Cell Value 基于单元格值循环遍历另一个工作簿中的范围 - Loop Through Ranges in Another Workbook Based on Cell Value 将基于单元格值的工作表移动到另一个工作簿 - Move Worksheet based off a cell value to another workbook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM