Pulling / Pushing Values from/to closed Excel files

pennylam

New Member
Joined
Feb 28, 2009
Messages
14
Hi all,

Well, 2 questions in a single thread in fact.

Pulling values
From the web I've found some scripts in form of subroutine which is capable for pulling values, listed in follows:

Sub Try_run()
extension = ".xlsx"
Cells(1, 1) = GetValue2(ThisWorkbook.path, Cells(2, 3) & extension, Cells(3, 3), Cells(2, 2).Address(0, 0))
End Sub

Private Function GetValue2(path, file, sheet, range_ref)
Dim arg As String
If Right(path, 1) <> "\" Then path = path & "\"
If Dir(path & file) = "" Then
GetValue2 = "-"
Exit Function
End If
arg = "'" & path & "[" & file & "]" & sheet & "'!" & _
Range(range_ref).Range("A1").Address(, , xlR1C1)
GetValue2 = ExecuteExcel4Macro(arg)
End Function

The above scripts works very well.
I try to make some changes but I can't get them correct for long...
1) the subroutine always pulls values from a fixed cell in the source worksheet (B2 in the above example)
how could this be change into a valuable in the running worksheet?
(e.g. in the running worksheet, a cell A2 as string which stores the value "B2", which could be change and re-run)
2) could the function GetValue2 be changed to a global function which works in excel outside VBA?


Pushing values
Search for a while on the web and they all point out it is impossible in excel.
but instead of pushing if it is possible to do the following in a workbook instead?
Subroutine in Worksheet A:
1) Open worksheet B
2) change certain cells
3) close worksheet B.
4) continue of on the subroutine

Sorry for such stupids questions.
Many thanks.
 
I don't think you can open a closed file within a function. I tried several ways without success.

if the file is open then you can get the value using the function.

Code:
Function MyGetVal(wb As String, sh As String, ad As String)
    Dim wb1 As Workbook
    Dim ws1 As Worksheet
    Dim Val
    Set wb1 = Workbooks(wb & ".xls")
    Set ws1 = wb1.Sheets(sh)
    Val = ws1.Range(ad).Value
    Set ws1 = Nothing
    Set wb1 = Nothing
    GetVal = Val
End Function
 
Upvote 0

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Forum statistics

Threads
1,225,149
Messages
6,183,178
Members
453,151
Latest member
Lizamaison

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top