2015-08-14 42 views

回答

1

不確定您是否可以在不打開工作簿的情況下執行此操作。我建立了一個如下所示的函數。

Private Function GetValue(path, file, sheet, ref) 
Dim wb As Workbook 
Application.ScreenUpdating = False 
Set wb = Workbooks.Open(path & Application.PathSeparator & file) 
Worksheets(sheet).Activate 
GetValue = Range(ref) 
wb.Close savechanges:=False 
Application.ScreenUpdating = True 
End Function 

然後使用它像

Dim path As String 
Dim file As String 
Dim sht As String 
Dim rng As String 
path = ThisWorkbook.path 
file = "book.XLSX" 
sht = "Sheet1" 
rng = "A1:D300" 

ActiveSheet.Range("A1:D300").Value = GetValue(path, file, sht, rng) 
+0

糟糕,你想要它在一個數組中。呃,嘗試存儲函數數組? XD – findwindow

+0

感謝您的快速回復。它的工作原理,仍然有興趣看看是否有人有解決方案,不需要打開外部工作簿。 –

+0

嗯可能http://stackoverflow.com/questions/24952856/pulling-data-from-a-closed-workbook-macro? – findwindow

1

如果你只在封閉的工作簿中的數據值感興趣,你可以用你打開的工作簿的空範圍爲臨時區域,然後填寫使用數組公式:

ScratchWorksheet.Range("B1", "B3").FormulaArray = "='PathToClosedBook\[ClosedBook.xlsx]Sheet1'!$B$1:$B$3" 

完成後,您可以刪除FormulaArray,以便在打開的工作簿中不保留外部鏈接。

相關問題