【问题标题】:Link to external workbook based on another cell链接到基于另一个单元格的外部工作簿
【发布时间】:2017-02-27 10:27:34
【问题描述】:

我正在处理 Excel 中的动态外部工作簿数据引用,请有人帮忙考虑以下问题:

在我的 A2 工作簿中,我有 ='C:\'&A1&' Reports\[1.xls]Sheet1'!C1)

A1 将包含任一

文件夹 A
文件夹 B

所以根据 A1 中的值,我想指向任一

D:\文件夹 A 报告\1.xls
D:\文件夹 B 报告\1.xls

我怎样才能做到这一点?

谢谢!

【问题讨论】:

  • 您可以使用 IF 语句或 VLOOKUP 表。这当然假设您只需要返回一个取决于查找值的值。
  • 感谢您的帮助!我只是希望在 A2 的路径中填充文件夹 A,就像调用变量一样。这种方式不可以吗?
  • 这当然是可能的,但我很抱歉,因为我不完全确定你想要完成什么。您当然可以根据 A1 的值填充路径,并且有几种方法可以实现这一点。正如我所指出的,vlookup 是一种方法。我有一种感觉,尽管您想要一个公式来自己找到路径,而无需在表格中提供它?这也是可能的,但这将进入 VBA 领域,而不仅仅是 excel 公式。

标签: excel excel-formula vba


【解决方案1】:

间接是你需要的功能:

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

我看到的问题是,如果工作表未打开,它将无法解决。

【讨论】:

  • 感谢您的回答。如果我硬编码路径,我可以简单地打开工作簿并关闭它,数据会更新......所以我编写了一个宏,可以打开我需要的所有电子表格并关闭它们......对于 INDIRECT 函数,电子表格必须保持打开状态,还是我可以使用相同的宏来打开和关闭我的外部源?
  • 打开它,刷新它,关闭它,它会保留答案。不过我不得不问,你为什么不直接从带有代码的工作表中复制数据并将其作为运行时不依赖的值放入目标工作表中?
  • 手动做太多了。很多标签和资源......迫不及待地想回家试试。再次感谢!
【解决方案2】:

由于INDIRECT 不适用于合上的书,而且打开它们的成本很高,因此正常的解决方案是

  1. 用 VBA 创建一个 dirty 链接。
  2. 使用 Morefunc 插件中的Indirect.Ext
  3. 我的首选方法是使用 XLM,Harlan Grove 在下面的 pull 函数中对其进行了改进。

用于您的目的:

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

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

拉取函数

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

【讨论】:

    【解决方案3】:

    Excel 公式中对已关闭文件的外部引用不能是动态的,所以可能是这样的:

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

    【讨论】:

      猜你喜欢
      • 2017-09-29
      • 2022-09-27
      • 2012-12-08
      • 2020-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多