2017-02-26 68 views
1

我的工作動態的外部工作簿的數據在Excel中引用的外部工作簿可以有人幫考慮以下幾點:鏈接到基於另一個細胞

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

A1將包含任一

文件夾A
文件夾B

所以取決於A1的價值,我想以指向

d:\文件夾A報告\ 1.xls
d:\文件夾B報告\ 1.xls

我該如何做到這一點?

謝謝!

+0

你可以使用一個IF語句或VLOOKUP表。這當然假設你只需要返回一個取決於查找值的值。 –

+0

謝謝你的幫助!我只是希望將文件夾A填充到A2的路徑中,就像調用變量一樣。這樣做不可能嗎? – Andrew

+0

這當然是可能的,但我很抱歉,因爲我不完全確定你要完成什麼。您當然可以根據A1的值填充路徑,並且有幾種方法可以實現此目的。正如我所指出的,查找是這樣做的一種方式。我覺得雖然你想要一個公式自己找到路徑,而不需要在表格中提供它。這也是可能的,但這將進入VBA的領域,而不僅僅是Excel的公式。 –

回答

2

間接是你需要的功能:

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

我看到,雖然問題是如果該表不是打開它不會解決。

+0

謝謝你的回答。如果我硬編碼的路徑,我可以簡單地打開工作簿,並關閉它,數據將更新...所以我寫了一個宏,將打開我需要的所有電子表格,並關閉它們...對於INDIRECT函數,電子表格必須保持打開狀態,還是可以使用相同的宏來打開和關閉我的外部源? – Andrew

+0

打開它,刷新它,關閉它,它將保持答案。我不得不問,爲什麼不直接從工作表中複製代碼中的數據,並將它作爲一個值放入目標工作表中,並且在運行時沒有依賴關係? –

+0

手動操作太多了。很多標籤和來源......迫不及待地回家嘗試一下。再次感謝! – Andrew

0

由於INDIRECT不能在封閉的書工作,它是有昂貴的開他們,那麼這種情況的正常soutions是

  1. 創建VBA一個鏈接。從Morefunc插件。
  2. 我的首選方法是用XLM其中Harlan Grove在他的下拉功能有所改進。

要使用你的目的:

=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 
0

到關閉文件的外部引用在Excel公式不能動態的,所以也許如此mething這樣的:

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