MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Feb 25th, 2002, 04:29 PM   #1
doron_angel
New Member
 
Join Date: Feb 2002
Posts: 8
Default

Sorry if this is the wrong forum & if has been asked before.

Is there a way to read a specific Cell value from a specific sheet (let's say "total") which is in another woorkbook (let's say file.xls) using VBA. Is it possible not to have this file opened and displayed?

Thanks in advance,

Doron.
doron_angel is offline   Reply With Quote
Old Feb 25th, 2002, 04:41 PM   #2
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

This is indeed the correct Forum.

For my example, I am trying to read the contents of Cell "A1" on "Sheet1" in "Book1.xls" that is saved in the "C:Temp" directory.


Dim XL As Excel.Application
Dim WBK As Excel.Workbook

Set XL = CreateObject("Excel.Application")
Set WBK = XL.Workbooks.Open("C:TempBook1.xls")
MsgBox WBK.Sheets("Sheet1").Range("A1").Value

WBK.Close
Set XL = Nothing



HTH

_________________
[b] Mark O'Brien

[ This Message was edited by: Mark O'Brien on 2002-02-25 15:42 ]

[ This Message was edited by: Mark O'Brien on 2002-02-25 15:55 ]
Mark O'Brien is offline   Reply With Quote
Old Feb 25th, 2002, 04:56 PM   #3
gplhl
Board Regular
 
Join Date: Feb 2002
Location: Chippenham, UK
Posts: 136
Default

Quote:
On 2002-02-25 15:29, doron_angel wrote:
Sorry if this is the wrong forum & if has been asked before.

Is there a way to read a specific Cell value from a specific sheet (let's say "total") which is in another woorkbook (let's say file.xls) using VBA. Is it possible not to have this file opened and displayed?

Thanks in advance,

Doron.
You can read the cell value no problem by using something like:

Sub readCellValue()
'Assigns the value to cellValue
cellValue = Range("[file.xls]total!A1").Value

End Sub

I'm not so sure about not having the book open, maybe someone else knows more about that?

If the book the value is in isn't too big then open it using vba, read the value and close it again. You can speed things up by turning off screen updating whilst running the script.

Sub readCellValue2()

Application.ScreenUpdating = False
Workbooks.Open ("C:file1.xls")
cellValue = Range("[file1.xls]total!A1").Value
ActiveWorkbook.Close
Application.ScreenUpdating = True

End Sub

Regards,

Gary Hewitt-Long
gplhl is offline   Reply With Quote
Old Feb 25th, 2002, 05:46 PM   #4
]-[ /-\ \/\/ /-\ | | /-\
New Member
 
Join Date: Feb 2002
Location: ]-[ /-\ \/\/ /-\ | | ~~~@|_()]-[/-\!!!
Posts: 48
Default

Many ways to extract data from closed workbook. I chose to use the following function written by David Hager (I'm not smart enough to come with this kind of stuff!):

_____________________________________________

Function CWRIA(fPath As String, fName As String, sName As String, rng As String)
Dim sRow As Integer
Dim sColumn As Integer
Dim sRows As Integer
Dim sColumns As Integer
Dim vrow As Integer
Dim vcol As Integer
Dim fpStr As String
Dim cArr()
On Error GoTo NoArr
If Right(fPath, 1) <> "" Then fPath = fPath & ""
If Dir(fPath & fName) = "" Then
CWA = CVErr(xlErrValue)
Exit Function
End If
sRow = Range(rng).Row
sColumn = Range(rng).Column
sRows = Range(rng).Rows.Count
sColumns = Range(rng).Columns.Count
ReDim cArr(sRows, sColumns)
For vrow = 1 To sRows
For vcol = 1 To sColumns
fpStr = "'" & fPath & "[" & fName & "]" & sName & "'!" & _
"r" & sRow + vrow - 1 & "c" & sColumn + vcol - 1
cArr(vrow, vcol) = ExecuteExcel4Macro(fpStr)
Next
Next
CWRIA = cArr
Exit Function
NoArr:
CWRIA = CVErr(xlErrValue)
End Function


Sub CWRIR(fPath As String, fName As String, sName As String, rng As String, destRngUpperLeftCell As String )
Dim sRow As Integer
Dim sColumn As Integer
Dim sRows As Integer
Dim sColumns As Integer
Dim vrow As Integer
Dim vcol As Integer
Dim fpStr As String
Dim cArr()
On Error GoTo NoArr
If Right(fPath, 1) <> "" Then fPath = fPath & ""
If Dir(fPath & fName) = "" Then
CWA = CVErr(xlErrValue)
Exit Function
End If
sRow = Range(rng).Row
sColumn = Range(rng).Column
sRows = Range(rng).Rows.Count
sColumns = Range(rng).Columns.Count
ReDim cArr(sRows, sColumns)
Set destRange = ActiveSheet.Range(destRngUpperLeftCell)
For vrow = 1 To sRows
For vcol = 1 To sColumns
fpStr = "'" & fPath & "[" & fName & "]" & sName & "'!" & _
"r" & sRow + vrow - 1 & "c" & sColumn + vcol - 1
destRange.Offset(vrow - 1, vcol - 1) = ExecuteExcel4Macro(fpStr)
Next
Next
NoArr:
End Sub

_____________________________________________

Cut and paste the above code into a module and then write your macro similar to below (in your case):

Sub ExtractDataSample()
Dim Filepath$, Filename$, Sheetname$, Src$, Dest$
'Source Information
Filepath = "C:pathwhereeveryourfileis"
Filename = "file.xls"
Sheetname = "total"
'Extract data from source range:
Src = "G5"
'Destination cell
Dest = "A1"
'insert line here if you need to activate your workbook/sheet
CWRIR Filepath, Filename, Sheetname, Src, Dest
End Sub


Change source info (path, filename, sheetname, & source cell range) to meet your needs. You can specify a single cell or an array as range. Code assumes that the destination cell is in the activeworkbook--which is why you only specify the cell--so you may have to add a line of code specifying which workbook worksheet to activate if it's not the activesheet at runtime. Hope it helps.

Aloha!
]-[ /-\ \/\/ /-\ | | /-\ is offline   Reply With Quote
Old Feb 25th, 2002, 06:19 PM   #5
brettvba
MrExcel MVP
 
Join Date: Feb 2002
Location: Christchurch New Zealand
Posts: 1,030
Default

Or you could just create a link and enable automatic link updates under edit links







Realize
brettvba is offline   Reply With Quote
Old Feb 26th, 2002, 01:43 AM   #6
Ivan F Moala
MrExcel MVP
 
Ivan F Moala's Avatar
 
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
Default

Quote:
On 2002-02-25 15:29, doron_angel wrote:
Sorry if this is the wrong forum & if has been asked before.

Is there a way to read a specific Cell value from a specific sheet (let's say "total") which is in another woorkbook (let's say file.xls) using VBA. Is it possible not to have this file opened and displayed?

Thanks in advance,

Doron.
Hi Doran

Here is yet another way

Sub test()
MsgBox GetValue("C:ExcelFilesUseful", "###.xls", "Sheet1", "A1")
End Sub


Private Function GetValue(Path, File, Sheet, Ref)

Dim sArg As String

If Right(Path, 1) <> "" Then Path = Path & ""

If Dir(Path & File) = "" Then
GetValue = "File Not Found"
Exit Function
End If

sArg = "'" & Path & "[" & File & "]" & Sheet & "'!"

MsgBox sArg

sArg = sArg & Range(Ref).Range("A1").Address(, , xlR1C1)

MsgBox sArg

GetValue = ExecuteExcel4Macro(sArg)

End Function

Ivan
Ivan F Moala is offline   Reply With Quote
Old Feb 26th, 2002, 01:21 PM   #7
doron_angel
New Member
 
Join Date: Feb 2002
Posts: 8
Default

Mark,

Thanks a million. It worked. However, do you happen to know how can I do a "quiet" workbook close (answer NO) to save changes?

Doron.

Quote:
On 2002-02-25 15:41, Mark O'Brien wrote:
This is indeed the correct Forum.

For my example, I am trying to read the contents of Cell "A1" on "Sheet1" in "Book1.xls" that is saved in the "C:Temp" directory.


Dim XL As Excel.Application
Dim WBK As Excel.Workbook

Set XL = CreateObject("Excel.Application")
Set WBK = XL.Workbooks.Open("C:TempBook1.xls")
MsgBox WBK.Sheets("Sheet1").Range("A1").Value

WBK.Close
Set XL = Nothing



HTH

_________________
[b] Mark O'Brien

[ This Message was edited by: Mark O'Brien on 2002-02-25 15:42 ]

[ This Message was edited by: Mark O'Brien on 2002-02-25 15:55 ]
doron_angel is offline   Reply With Quote
Old Feb 26th, 2002, 01:57 PM   #8
Russell Hauf
MrExcel MVP
 
Russell Hauf's Avatar
 
Join Date: Feb 2002
Location: Portland, OR USA
Posts: 1,374
Default

Just use

WBK.Close False

To not save changes.

-rh
Russell Hauf is offline   Reply With Quote
Old Feb 27th, 2002, 11:07 AM   #9
doron_angel
New Member
 
Join Date: Feb 2002
Posts: 8
Default

Thanks, it worked!

Quote:
On 2002-02-26 12:57, Russell Hauf wrote:
Just use

WBK.Close False

To not save changes.

-rh
doron_angel is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 01:47 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes