+ Reply to Thread
Results 1 to 2 of 2

How to grab the value of a Named Range (Named Cell) from another worksheet using ADO?

  1. #1
    Registered User
    Join Date
    08-28-2012
    Location
    New Jersey
    MS-Off Ver
    Excel 2010
    Posts
    18

    How to grab the value of a Named Range (Named Cell) from another worksheet using ADO?

    Hello,

    I am using Excel 2013. I need to grab the value of a named cell from another workbook (without opening it). I've searched and read A LOT! I've tried, but I cannot get it to work. Here's what I have so far:

    Sub ConnectToExcelUsingSheetName()


    On Error GoTo ConnectToExcelUsingSheetName_Err

    ' Declare variables
    Dim cnn As New ADODB.Connection
    Dim rs As New ADODB.Recordset
    Dim query As String

    ' Open the connection then open the recordset
    cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
    "Data Source=C:\Temp\PM Workbook - Test.xlsm;" & _
    "Extended Properties=""Excel 12.0 Xml;IMEX=1"";"

    ' Notice I dropped "HDR" to see if that would make a difference as the named cell does not use not require a header.
    ' "Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1"";"
    ' "Extended Properties=""Excel 12.0 Xml;HDR=YES;"";"



    query = "SELECT * FROM Named_Cell_A1" 'this should work according to what I've read online
    'query = "SELECT * FROM [Named_Cell_A1]" 'tried this is case I needed the brackets... still does not work. No trailing "$" so it's not a sheet... it's a named cell


    rs.Open query, cnn, adOpenStatic, adLockReadOnly
    'rs.Open "SELECT * FROM Named_Cell_A1;", cnn, adOpenStatic, adLockReadOnly
    'rs.Open query, cnn

    'The following part does NOT WORK. Seems the named range was not ready??

    'Worksheets("Sheet1").Range("B1").CopyFromRecordset rs
    Sheet1.Range("B1").CopyFromRecordset rs
    MsgBox "B1 = " & Sheet1.Range("B1").Value



    'Debug.Print rs

    ' Tidy up
    ConnectToExcelUsingSheetName_Exit:
    On Error Resume Next
    rs.Close
    cnn.Close
    Set rs = Nothing
    Set cnn = Nothing
    Exit Sub

    ConnectToExcelUsingSheetName_Err:
    MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error!"
    Resume ConnectToExcelUsingSheetName_Exit

    End Sub



    I know others are using this ADO technique. I must be doing something basic incorrectly. Any help is greatly appreciated! Thank you!

    NJDevil
    Attached Files Attached Files

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,873

    Re: How to grab the value of a Named Range (Named Cell) from another worksheet using ADO?

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    Also, please update your profile to reflect that you are currently running 2013. Click on User CP to make that change.

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

+ 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. Listbox displaying named range B but Adding named range A to cell
    By ikkenieikke in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-05-2018, 02:27 PM
  2. Adjust existing macro to pick up specific named worksheet and copy to named range
    By Groovicles in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-08-2017, 12:03 PM
  3. [SOLVED] Assign named range to one cell depending on named range in another
    By x65140 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-22-2015, 11:04 AM
  4. [SOLVED] determining if cell is part of named range and what that named range is
    By stnkynts in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-16-2014, 07:56 PM
  5. [SOLVED] Find Value in Sheet Named After Cell and Grab Specific Cell Value
    By Caulerpa in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 10-21-2013, 02:21 PM
  6. [SOLVED] Determining if the value of a cell can be a named range, then assigning named ranges after
    By Romulo in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-15-2013, 06:05 PM
  7. Replies: 1
    Last Post: 06-03-2006, 10:55 PM

Tags for this Thread

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