Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 30
Default Get data from closed workbook for use in a variable (no copying)

All the threads on this subject are about copying data from a closed workbook
to an active workbook. What I would like to do is just pickup the value of a
specific cell in a specific sheet in a closed workbook and use that value in
a variable for processing purposes. What can I use to do this?

Thanks

Bob
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Get data from closed workbook for use in a variable (no copying)

This should do the job:


Sub test()

MsgBox GetValueFromWB("C:\", "WB_Value_Test.xls", "Sheet1", "B2")

End Sub

Function GetValueFromWB(strPath As String, _
strFile As String, _
strSheet As String, _
strRef As String) As Variant

'Retrieves a value from a closed workbook
'----------------------------------------
Dim strArg As String

'make sure we have the trailing backslash
'----------------------------------------
If Right$(strPath, 1) < "\" Then
strPath = strPath & "\"
End If

'Make sure the file exists
'-------------------------
If bFileExists(strPath & strFile) = False Then
GetValueFromWB = "File Not Found"
Exit Function
End If

'Create the argument
'-------------------
strArg = "'" & strPath & "[" & strFile & "]" & strSheet & "'!" & _
Range(strRef).Range("A1").Address(, , xlR1C1)

'Execute an XLM macro
'--------------------
GetValueFromWB = ExecuteExcel4Macro(strArg)

End Function

Function bFileExists(ByVal sFile As String) As Boolean

Dim lAttr As Long

On Error Resume Next
lAttr = GetAttr(sFile)
bFileExists = (Err.Number = 0) And ((lAttr And vbDirectory) = 0)
On Error GoTo 0

End Function


RBS


"Bob Zimski" wrote in message
...
All the threads on this subject are about copying data from a closed
workbook
to an active workbook. What I would like to do is just pickup the value of
a
specific cell in a specific sheet in a closed workbook and use that value
in
a variable for processing purposes. What can I use to do this?

Thanks

Bob


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,069
Default Get data from closed workbook for use in a variable (no copying)

You can do it using Formula.

Give this a try & see if works for you.

Sub GetValue()
Dim mydata As String
'data location & range to copy
mydata = "='C:\[Mybook.xls]Sheet1'!$B$2" '<< change as required

'link to worksheet
With ThisWorkbook.Worksheets(1).Range("B2:") '<< change as required
.Formula = mydata

'convert formula to text
.Copy
.PasteSpecial Paste:=xlPasteValues
End With
Application.CutCopyMode = False
End Sub
--
jb


"Bob Zimski" wrote:

All the threads on this subject are about copying data from a closed workbook
to an active workbook. What I would like to do is just pickup the value of a
specific cell in a specific sheet in a closed workbook and use that value in
a variable for processing purposes. What can I use to do this?

Thanks

Bob

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 30
Default Get data from closed workbook for use in a variable (no copyin

It works like a charm.

Wow, I guess there is not direct function.

I needed to do this because I wanted to store specifc defaults in a separate
workbook for various macros I have where things may be different based onthe
user.

Thanks much!

Bob

"RB Smissaert" wrote:

This should do the job:


Sub test()

MsgBox GetValueFromWB("C:\", "WB_Value_Test.xls", "Sheet1", "B2")

End Sub

Function GetValueFromWB(strPath As String, _
strFile As String, _
strSheet As String, _
strRef As String) As Variant

'Retrieves a value from a closed workbook
'----------------------------------------
Dim strArg As String

'make sure we have the trailing backslash
'----------------------------------------
If Right$(strPath, 1) < "\" Then
strPath = strPath & "\"
End If

'Make sure the file exists
'-------------------------
If bFileExists(strPath & strFile) = False Then
GetValueFromWB = "File Not Found"
Exit Function
End If

'Create the argument
'-------------------
strArg = "'" & strPath & "[" & strFile & "]" & strSheet & "'!" & _
Range(strRef).Range("A1").Address(, , xlR1C1)

'Execute an XLM macro
'--------------------
GetValueFromWB = ExecuteExcel4Macro(strArg)

End Function

Function bFileExists(ByVal sFile As String) As Boolean

Dim lAttr As Long

On Error Resume Next
lAttr = GetAttr(sFile)
bFileExists = (Err.Number = 0) And ((lAttr And vbDirectory) = 0)
On Error GoTo 0

End Function


RBS


"Bob Zimski" wrote in message
...
All the threads on this subject are about copying data from a closed
workbook
to an active workbook. What I would like to do is just pickup the value of
a
specific cell in a specific sheet in a closed workbook and use that value
in
a variable for processing purposes. What can I use to do this?

Thanks

Bob



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
How to pass variable between closed workbook hon123456 Excel Programming 0 June 13th 06 10:20 AM
Getting data from a closed workbook Baapi Excel Programming 5 April 11th 06 07:52 AM
loop through a column on a workbook copying data on each row to another workbook, then copy data back to the original workbook burl_rfc Excel Programming 1 April 1st 06 08:48 PM
PUT data in closed workbook Max Potters Excel Programming 5 August 18th 04 01:33 PM
Copying Data from closed workbooks Kevin G Excel Programming 4 July 31st 03 03:46 PM


All times are GMT +1. The time now is 02:42 PM.

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

About Us

"It's about Microsoft Excel"