2012-10-24 45 views
0

說我有在從另一個工作簿Excel公式如何連接字符串外部參考

='c:\temp\[external book.xlsx]SheetX'!$E$4 

讀取單元格的值單元格下面的公式,我想的c:\temp\[external book.xlsx]SheetX價值來自另一個細胞在這張紙。我將如何改寫這個公式來連接該值和"!$E$4"

可以說細胞A1包含c:\temp\[external book.xlsx]SheetX

+0

是否有可能寫爲Excel函數,可以從細胞中稱爲其中對於A1 = INDIRECT_2小區式(A2)和A2的值='c:\ temp \ [external book.xlsx] SheetX'!$ E $ 4 – JustinJDavies

回答

2

編輯:如下的不會在關閉的工作簿的工作,這裏是一個笨重證明的如何你可以用VBA做-concept(我想有這樣做的更好的方法):

Sub ClosedWorkbook() 

Dim currSht As Worksheet 
Dim targetWbk As Workbook 
Dim Path As String 

' Turn off updating 
Application.ScreenUpdating = False 

' Set a reference to the current sheet 
Set currSht = ActiveWorkbook.ActiveSheet 

' Get the value in the formula cell (A1 here, could be ActiveCell if in a loop, etc.) 
PathSheet = currSht.Range("A1").Value 

' Split out the path - this is very clunky, more of a proof (?) of concept 
' This is depends on the path being as you mentioned, and the IF block is to 
' check if the apostrophe is present in the cell (since it is the escape character, 
' it is usually skipped) 
If Left(PathSheet, 1) = "'" Then 
    Path = Replace(Mid(PathSheet, 2, InStr(PathSheet, "]") - 2), "[", "") 
    Sheet = Mid(PathSheet, InStr(PathSheet, "]")) 
    Sheet = Left(Sheet, Len(Sheet) - 2) 
Else 
    Path = Replace(Mid(PathSheet, 1, InStr(PathSheet, "]") - 1), "[", "") 
    Sheet = Mid(PathSheet, InStr(PathSheet, "]") + 1) 
    Sheet = Left(Sheet, Len(Sheet) - 2) 
End If 

' Open the target workbook 
Set targetWbk = Workbooks.Open(Path) 

' Grab the value from E4 and drop it in a cell (D1 here, could be ActiveCell, etc.) 
MyValue = targetWbk.Sheets(Sheet).Range("E4").Value 
currSht.Range("D5").Value = MyValue 

' Close the target 
targetWbk.Close 

Application.ScreenUpdating = True 
End Sub 

老辦法(不適用於封閉的工作簿)

我相信這會捕獲您要查找的內容。在這個例子中,小區A1有我的片材路徑:

'C:\temp\[externalworkbook.xlsx]Sheet1'! 

單元B1我具有下式:

=INDIRECT(A1 & "$E$4") 

其中在爲了產生完整的路徑,這與$E$4符連接片值然後通過INDIRECT轉換爲值。有一點要注意:撇號是一個轉義字符,所以這取決於你的數據,你可能要在公式中專門帳戶:

=INDIRECT("'" & A1 & "$E$4") 

如果您使用Excel 2007或以上,你可以換它在一個IFERROR式取出猜測:

=IFERROR(INDIRECT(A1 & "$E$4"),INDIRECT("'" & A1 & "$E$4")) 
+0

@PreetSangha是的,意思是放在那裏。我會看看我是否可以想到一些關閉的東西。 – RocketDonkey

+1

謝謝,但如果外部工作簿(a)關閉或(b)在不同Excel實例中打開外部工作簿,間接不起作用 –

+0

@PreetSangha VBA是一個選項嗎?另外,我會在這篇文章中加上'excel'標籤 - 有些人比我更好,他們可能會有更好的想法。 – RocketDonkey