Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Put cell value from another workbook into a variable


I want my visual basic code to get the value of a cell in another
workbook and put into a variable. Here is an example that does not
work:

Fund1Shares = "'E:\Finance\[Fund.xls]Fund'!$B$3"

When I write the value of Fund1Shares to a message box I get
'E:\Finance\[Fund.xls]Fund'!$B$3 and the not the value in $B$3

Any help would be appreciated.


--
rdshadden
------------------------------------------------------------------------
rdshadden's Profile: http://www.excelforum.com/member.php...o&userid=34778
View this thread: http://www.excelforum.com/showthread...hreadid=546595

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Put cell value from another workbook into a variable

Hi

The following should do what you're after.

All the best

David

=======

Sub extlink()
Dim extvalue As String 'change this to integer / long / double /
whatever

'remove the next line if the fund spreadsheet is always open when you
want to get the value
Application.Workbooks.Open ("e:\finance\fund.xls") 'open the file to
get the value

'the next line gets the required value
extvalue = Workbooks("fund.xls").Worksheets("fund").Range("C3 ").Value

'remove the next line if you don't want to close the file when you've
got the value
Workbooks("fund.xls").Close 'close the newly opened file as it's not
needed

'the next line displays the retrieved value
MsgBox (extvalue)

End Sub

=====

"rdshadden" wrote
in message ...

I want my visual basic code to get the value of a cell in another
workbook and put into a variable. Here is an example that does not
work:

Fund1Shares = "'E:\Finance\[Fund.xls]Fund'!$B$3"

When I write the value of Fund1Shares to a message box I get
'E:\Finance\[Fund.xls]Fund'!$B$3 and the not the value in $B$3

Any help would be appreciated.


--
rdshadden
------------------------------------------------------------------------
rdshadden's Profile:
http://www.excelforum.com/member.php...o&userid=34778
View this thread: http://www.excelforum.com/showthread...hreadid=546595



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 923
Default Put cell value from another workbook into a variable

To get an external workbook value you must open it first, get the value(s)
then close it. .... try this

Sub getvalue()

Dim aWB As Workbook, bWB As Workbook, Fund1Shares
Set aWB = ActiveWorkbook

Workbooks.Open Filename:="E:\Finance\Fund.xls"
Set bWB = ActiveWorkbook

Fund1Shares = bWB.Sheets("Fund").Range("B3")

bWB.Close

MsgBox Fund1Shares

End Sub

--
Cheers
Nigel



"rdshadden" wrote
in message ...

I want my visual basic code to get the value of a cell in another
workbook and put into a variable. Here is an example that does not
work:

Fund1Shares = "'E:\Finance\[Fund.xls]Fund'!$B$3"

When I write the value of Fund1Shares to a message box I get
'E:\Finance\[Fund.xls]Fund'!$B$3 and the not the value in $B$3

Any help would be appreciated.


--
rdshadden
------------------------------------------------------------------------
rdshadden's Profile:
http://www.excelforum.com/member.php...o&userid=34778
View this thread: http://www.excelforum.com/showthread...hreadid=546595



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Put cell value from another workbook into a variable

John Walkenbach has a routine that can get values from a closed workbook:
http://j-walk.com/ss/excel/eee/eee009.txt
Look for either: GetDataFromClosedFile or GetValue.

And you could also just build a formula in an empty cell, retrieve the value and
then clean up that helper cell.

rdshadden wrote:

I want my visual basic code to get the value of a cell in another
workbook and put into a variable. Here is an example that does not
work:

Fund1Shares = "'E:\Finance\[Fund.xls]Fund'!$B$3"

When I write the value of Fund1Shares to a message box I get
'E:\Finance\[Fund.xls]Fund'!$B$3 and the not the value in $B$3

Any help would be appreciated.

--
rdshadden
------------------------------------------------------------------------
rdshadden's Profile: http://www.excelforum.com/member.php...o&userid=34778
View this thread: http://www.excelforum.com/showthread...hreadid=546595


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading Data from another workbook... depending on variable in a cell? Rob Moyle Excel Discussion (Misc queries) 4 March 13th 06 04:21 PM
Running a variable macro when any value is entered into a variable cell [email protected] Excel Programming 3 December 14th 05 05:15 PM
Linking a cell to another workbook cell based on a variable name Brian Excel Discussion (Misc queries) 6 June 1st 05 11:54 PM
Can a workbook variable be set by clicking the workbook? Kobayashi[_46_] Excel Programming 0 October 28th 04 08:55 PM
Can a workbook variable be set by clicking the workbook? Kobayashi[_45_] Excel Programming 1 October 28th 04 08:11 PM


All times are GMT +1. The time now is 11:44 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"