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
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
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
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.
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!
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
Hummm... no quite getting what u mean there Jimmy...Originally Posted by jimmike1
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
>
"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
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.Originally Posted by mattsvai
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)
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 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".Please Login or Register to view this content.
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.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks