Results 1 to 18 of 18

VBA to read a single value from a closed workbook

Threaded View

  1. #1
    Registered User
    Join Date
    07-27-2024
    Location
    Perth
    MS-Off Ver
    Office Professional Plus 2016
    Posts
    5

    VBA to read a single value from a closed workbook

    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
    Attached Files Attached Files
    Last edited by AliGW; 07-31-2024 at 02:09 AM. Reason: Code tags added - please review the forum guidelines.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Read data from closed workbook
    By vegkol in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 02-22-2021, 09:13 AM
  2. How to read update from a closed workbook
    By Gooford in forum Excel General
    Replies: 3
    Last Post: 12-11-2012, 10:27 AM
  3. Read a range from a closed workbook
    By danny2000 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-14-2010, 06:07 AM
  4. Open and read closed workbook
    By Phrone in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-11-2009, 03:55 PM
  5. How to Read/Write Data to/from a closed workbook
    By samtwilliams in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-11-2008, 10:02 AM
  6. [SOLVED] Read/Write from/to a closed workbook
    By Alan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-13-2006, 09:00 AM
  7. [SOLVED] How to read from a closed workbook?
    By matelot in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-05-2005, 02:45 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1