+ Reply to Thread
Results 1 to 18 of 18

VBA to read a single value from a closed workbook

Hybrid View

Rob A VBA to read a single value... 07-28-2024, 12:02 AM
TMS Re: VBA to read a single... 07-28-2024, 04:20 AM
Rob A Re: VBA to read a single... 07-28-2024, 05:55 AM
Rob A TMS would you be kind enough... 07-29-2024, 12:27 AM
JEC. Re: VBA to read a single... 07-28-2024, 04:56 AM
Rob A Re: VBA to read a single... 07-28-2024, 05:57 AM
JEC. Re: VBA to read a single... 07-28-2024, 06:09 AM
Rob A Thanks JEC, I understand... 07-29-2024, 12:17 AM
TMS Re: VBA to read a single... 07-31-2024, 02:07 AM
JEC. Re: VBA to read a single... 07-31-2024, 02:42 AM
Sintek Re: VBA to read a single... 07-31-2024, 03:42 AM
romperstomper Re: VBA to read a single... 07-31-2024, 08:10 AM
JEC. Re: VBA to read a single... 07-31-2024, 08:47 AM
TMS Re: VBA to read a single... 07-31-2024, 07:28 PM
romperstomper Re: VBA to read a single... 08-01-2024, 04:33 AM
Sintek Re: VBA to read a single... 08-01-2024, 06:13 AM
JEC. Re: VBA to read a single... 08-01-2024, 06:35 AM
Sintek Re: VBA to read a single... 08-01-2024, 06:37 AM
  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.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,517

    Re: VBA to read a single value from a closed workbook

    I haven't tested it ( working on an iPad ) but it looks like it should work. The obvious flaw is that it opens the workbook, gets the value and closes the workbook without saving it.

    Maybe try this instead: https://www.codevba.com/excel/closed..._get_value.htm
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


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

    Re: VBA to read a single value from a closed workbook

    Thanks TMS, I couldn't get this to work either. Still got that annoying #VALUE! error.

  4. #4
    Registered User
    Join Date
    07-27-2024
    Location
    Perth
    MS-Off Ver
    Office Professional Plus 2016
    Posts
    5
    Quote Originally Posted by Rob A View Post
    Thanks TMS, I couldn't get this to work either. Still got that annoying #VALUE! error.

    TMS would you be kind enough to test your solution and let me know whether it's a me problem. Thank you, Rob

  5. #5
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: VBA to read a single value from a closed workbook

    How about

    Sub jec()
    [c11].Formula = "='" & [D2] & "[" & [D3] & "]" & [D5] & "'!A1"
    [c11].Value = [c11].Value
    End Sub
    or

    Sub jec()
    [c11] = ExecuteExcel4Macro("'" & [D2] & "[" & [D3] & "]" & [D5] & "'!R1C1")
    End Sub
    Last edited by JEC.; 07-28-2024 at 05:05 AM.

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

    Re: VBA to read a single value from a closed workbook

    JEC, sorry I'm not great with VBA. How do I call this routine and what's the significance of [c11]?

  7. #7
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: VBA to read a single value from a closed workbook

    Alt +F11 to open vba, right click on the project and insert new module. Paste one of the macro there and hit run.

    [c11] means cell C11.

  8. #8
    Registered User
    Join Date
    07-27-2024
    Location
    Perth
    MS-Off Ver
    Office Professional Plus 2016
    Posts
    5
    Quote Originally Posted by JEC. View Post
    Alt +F11 to open vba, right click on the project and insert new module. Paste one of the macro there and hit run.

    [c11] means cell C11.

    Thanks JEC, I understand better now. In the general case then where for example in cell D11 I have another combination of filename, worksheet and cell reference, I would need to write multiple lines of code to accommodate. Tricky isn't it. Are there alternative options do you know?

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,517

    Re: VBA to read a single value from a closed workbook

    Bump.

    I'm not having a lot of success testing the solutions.

    Anyone else got any ideas?

  10. #10
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: VBA to read a single value from a closed workbook

    You don't need to write multiple lines of code if you set up the references in your worksheet like the first one.
    It could be written as a function ofcourse but you can't use the function directly in the worksheet unfortunately
    Last edited by JEC.; 07-31-2024 at 02:59 AM.

  11. #11
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: VBA to read a single value from a closed workbook

    I would like to be able to pass the folder location, excel filename, sheet reference and cell reference
    Assuming you have these 4 values above in ranges B1, B2, B3, B4

    D:\Steven\Desktop\
    FORM.xlsx
    Sheet1
    A1

    Then this as per above will work...For other references in other cells you could add a loop...
    Sub J3v16()
    Dim Valu, FilePath As String, File As String, Sht As String, Cell As String
    FilePath = Range("B1"): File = Range("B2").Value: Sht = Range("B3"): Cell = Range("B4")
    Valu = ExecuteExcel4Macro("'" & FilePath & "[" & File & "]" & Sht & "'!" & Range(Cell).Range("A1").Address(, , xlR1C1))
    End Sub
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  12. #12
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,015

    Re: VBA to read a single value from a closed workbook

    You could do it with ADO - something like:

    Function GetValueFromClosedWorkbook(filePath As String, sheetName As String, cellAddress As String)
       Const adOpenStatic As Long = 3
       Const adLockReadOnly As Long = 1
       Const adCmdText As Long = 1
       Const ACE_OLEDB_PROVIDER As String = "Microsoft.ACE.OLEDB.12.0"
       Dim cn As Object
       Set cn = CreateObject("ADODB.Connection")
       
       With cn
          .Provider = ACE_OLEDB_PROVIDER
          .ConnectionString = "Data Source=" & filePath & ";" & _
          "Extended Properties=""Excel 12.0 XML;HDR=No;"""
          .Open
       End With
       Dim index As Long
       If Range(cellAddress).Row = Rows.Count Then
          cellAddress = Range(cellAddress).Offset(-1).Resize(2).Address(0, 0)
          index = 1
       Else
          cellAddress = Range(cellAddress).Resize(2).Address(0, 0)
          index = 0
       End If
       Dim Query As String
       Query = "SELECT * FROM [" & sheetName & "$" & cellAddress & "]"
       
       With CreateObject("ADODB.Recordset")
          .Open Query, cn, adOpenStatic, adLockReadOnly, adCmdText
          If Not .EOF Then GetValueFromClosedWorkbook = .getrows()(0, index)
          .Close
       End With
       cn.Close
    End Function
    Everyone who confuses correlation and causation ends up dead.

  13. #13
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: VBA to read a single value from a closed workbook

    Function jec(fPath, fName, shtName, refCell)
     Dim xDriver, sqlString
     xDriver = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & fPath & fName & ";Extended Properties=""Excel 12.0;HDR=No;"""
     sqlString = "SELECT * FROM [" & shtName & "$" & Range(refCell).Resize(2).Address(0, 0) & "]"
    
     With CreateObject("ADODB.Recordset")
       .Open sqlString, xDriver
        jec = .getrows()(0, 0)
       .close
     End With
    End Function

  14. #14
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,517

    Re: VBA to read a single value from a closed workbook

    Here's a test bed workbook with RomperStomper and jec's solutions demonstrated.
    Attached Files Attached Files

  15. #15
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,015

    Re: VBA to read a single value from a closed workbook

    Just note that jec's version will not work with any cell in the last row of the sheet (unlikely, I know )

    And thank you for the rep Trevor.

    I think something must have changed with the ExecuteExcel4Macro workings these days as I'm sure that code used to work.

  16. #16
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: VBA to read a single value from a closed workbook

    I think something must have changed with the ExecuteExcel4Macro workings these days
    Works perfectly for me...

  17. #17
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: VBA to read a single value from a closed workbook

    It does but not as function

  18. #18
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: VBA to read a single value from a closed workbook

    Oh of course...Misunderstood...Tx for clarifying JEC...

+ Reply to Thread

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