Hello excel lovers,
I am trying to solve an age old problem of reading a single value from a closed excel file into my current, open workbook. I realise that the Indirect function doesn't work with closed files and have found the below formula and VBA on the web.
The below formula is useful but static. I would like to be able to pass the folder location, excel filename, sheet reference and cell reference from information contained in my open workbook.
='C:\path\to\your\folder\[Book1.xlsx]Sheet1'!A1
The below VBA
Function GetValueFromClosedWorkbook(filePath As String, sheetName As String, cellAddress As String)
Dim wb As Workbook
Set wb = Workbooks.Open(filePath, False, True) ' Open as read-only
GetValueFromClosedWorkbook = wb.Sheets(sheetName).Range(cellAddress).Value
wb.Close False ' Close without saving
End Function
and function call in excel, is as close as I can find, but doesn't work for me. With Value error not being easy to resolve.
=GetValueFromClosedWorkbook("C:\path\to\your\folder\Book1.xlsx", "Sheet1", "A1")
Would anyone be able to correct the above or suggest an alternative for me?
When calling this function I am using =GetValueFromClosedWorkbook(B1,B2,B3) where cell references B1, B2, and B3 in my current, open workbook, contain text strings of path (including file name), sheet and cell reference respectively.
Thank you,
Rob
Bookmarks