+ Reply to Thread
Results 1 to 10 of 10

Basic VBA question...

  1. #1
    Registered User
    Join Date
    02-03-2006
    Location
    Canada
    Posts
    18

    Basic VBA question...

    How can I pull data from a specific cell from a closed worksheet in VBA?

    Not sure but I think that Workbook("wb1.xls").Worksheet("Sheet1").Range("A2") only works if the worksheet is open.

    Any help would be appreciated,

    Matts

  2. #2
    Crowbar via OfficeKB.com
    Guest

    Re: Basic VBA question...

    If the workbook is closed you need to add the full path

    this is a cell formula not a macro

    ='C:\YourDirectory\[yourfile.xls]Sheet1'!A1

    You may need to change sheet1 to the name of your sheet and A1 is the cell
    you are looking for,

    --
    Message posted via OfficeKB.com
    http://www.officekb.com/Uwe/Forums.a...mming/200602/1

  3. #3
    Registered User
    Join Date
    02-01-2006
    Posts
    4
    The simplest way would be to establish a link from the closed workbook to the open book. Your macro then could read the value of the linked range.

  4. #4
    Registered User
    Join Date
    02-03-2006
    Location
    Canada
    Posts
    18
    Thanks for the reply,

    unfortunately I still get the nasty "Subscript out of range" error when I do that:

    var = Workbooks("C:\User1.xls").Worksheets("Record").Range("A2")

    ARRGG!

  5. #5

    Re: Basic VBA question...

    Hello Matts,

    I suggest to look here:
    http://www.erlandsendata.no/english/...acimportadotxt

    The author provides other load programs, too - but this one should work
    for your .xls files.

    Regards,
    Bernd


  6. #6
    Registered User
    Join Date
    02-03-2006
    Location
    Canada
    Posts
    18
    Quote Originally Posted by jimmike1
    The simplest way would be to establish a link from the closed workbook to the open book. Your macro then could read the value of the linked range.
    Hummm... no quite getting what u mean there Jimmy...

  7. #7
    Tom Ogilvy
    Guest

    Re: Basic VBA question...

    No, adding a path will not work. The workbooks collection is only for open
    workbooks. there is no open workbook with a name of
    Workbooks("C:\User1.xls"), so you get subscript out of range.

    VBA doesn't support direct references to closed workbooks.

    If you only want a single value, the fastest is to put a link in a cell and
    pick up the value returned. You can clen clear the link.

    If your data in the closed workbook is organized like a database, you can
    use ADO to retrieve the data.

    --
    Regards,
    Tom Ogilvy


    "mattsvai" <[email protected]> wrote in
    message news:[email protected]...
    >
    > Thanks for the reply,
    >
    > unfortunately I still get the nasty "Subscript out of range" error when
    > I do that:
    >
    > var = Workbooks("C:\User1.xls").Worksheets("Record").Range("A2")
    >
    > ARRGG!
    >
    >
    > --
    > mattsvai
    > ------------------------------------------------------------------------
    > mattsvai's Profile:

    http://www.excelforum.com/member.php...o&userid=31134
    > View this thread: http://www.excelforum.com/showthread...hreadid=508116
    >




  8. #8
    Bruno Campanini
    Guest

    Re: Basic VBA question...

    "mattsvai" <[email protected]> wrote in
    message news:[email protected]...
    >
    > Thanks for the reply,
    >
    > unfortunately I still get the nasty "Subscript out of range" error when
    > I do that:
    >
    > var = Workbooks("C:\User1.xls").Worksheets("Record").Range("A2")
    >
    > ARRGG!


    No way for Excel VBA to read closed workbooks unless you
    create a DAO/ADO recordset from the closed file.

    If your need is for a single cell reading you can send the formula
    ='C:\YourDirectory\[yourfile.xls]Sheet1'!A1
    to a cell and then read that cell.

    e.g.
    [H90].Formula = "='C:\Document\Excel\XLS\[BBCC.xls]Sheet1'!E15"
    MsgBox [H90]

    Bruno




  9. #9
    Registered User
    Join Date
    02-01-2006
    Posts
    4
    Quote Originally Posted by mattsvai
    Hummm... no quite getting what u mean there Jimmy...
    Designate a range in your open workbook as a link to the value you want in the closed one. It would be helpful to name the range containing the needed value. Name it, for example, WeeklySales.Then the formula for the range in the open book would look like this: =ClosedBook!WeeklySales.

    Your VBA procedure would access the range in the open book. Say it is Range B4 in a sheet called Sales.

    It would simply refer to Range("Sales!B4").Value (or whatever)

  10. #10
    Registered User
    Join Date
    03-24-2008
    Location
    Calabar, Nigeria
    MS-Off Ver
    2003, 2007
    Posts
    37

    Grab Data From a Single Cell in a Closed Workbook

    Linking a cell in your open book to the cell in your closed book and referencing it in VBA works but it is clumsy and will eat up resources if you use too many of them. Then there is the nagging annoyance of the update dialog box. The way to go is ADO. It is neat and efficient once you get it right. Below, find a generic code I use always.

    Please Login or Register  to view this content.
    Please note that the parameter ExcelJetVersion, though related, is not the same as the Excel version. e.g. the default "8.0" is valid for Excel 2000/2002/2003. Excel 97 has a Jet version of "97".

    If you omit the sheet name, it is assumed you have only one worksheet in the workbook and the worksheet with the 1st index will be used.

    You may go to the following for more info
    http://www.fontstuff.com/ebooks/free...nnectExcel.pdf
    Last edited by nsenor udofa; 03-25-2008 at 07:00 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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