Code to grab values from a closed workbook

wut

Banned
Joined
Dec 13, 2010
Messages
229
This is supposedly working code, but it doesn't work for me, and I don't understand how it's supposed to work. Can anyone help with that?


Code:
    GetValuesFromAClosedWorkbook "C:", "Book1.xls", _
            "Sheet1", "A1:K30"
End Sub
 
Sub GetValuesFromAClosedWorkbook(fPath As String, _
            fName As String, sName, cellRange As String)
    With ActiveSheet.Range(cellRange)
        .FormulaArray = "='" & fPath & "\[" & fName & "]" _
                    & sName & "'!" & cellRange
        .Value = .Value
    End With
End Sub

Edit: What I mean when I say I don't understand it is that all the variables are never defined. They're never linked to the function's arguments, so how can the code recognize where it's supposed to be looking to fill "fPath", "fName", "sName" and "cellRange"...?

A lot of the tips I'm finding to grab values from a closed workbook are like this. Nothing to set the variables, I don't get how that can work.
 
Last edited:

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
This is supposedly working code, but it doesn't work for me, and I don't understand how it's supposed to work. Can anyone help with that?


Code:
    GetValuesFromAClosedWorkbook "C:", "Book1.xls", _
            "Sheet1", "A1:K30"
End Sub
 
Sub GetValuesFromAClosedWorkbook(fPath As String, _
            fName As String, sName, cellRange As String)
    With ActiveSheet.Range(cellRange)
        .FormulaArray = "='" & fPath & "\[" & fName & "]" _
                    & sName & "'!" & cellRange
        .Value = .Value
    End With
End Sub

Edit: What I mean when I say I don't understand it is that all the variables are never defined. They're never linked to the function's arguments, so how can the code recognize where it's supposed to be looking to fill "fPath", "fName", "sName" and "cellRange"...?

A lot of the tips I'm finding to grab values from a closed workbook are like this. Nothing to set the variables, I don't get how that can work.

Hi wut,
You have partially quoted the first macro, and that's the key.
The first macro, Test, provides the values
The second macro, GetValuesFromAClosedWorkbook, is called by the Test, and Test passes the values of fPath As String, fName As String, sName, cellRange As String to GetValuesFromAClosedWorkbook.
the full code to use is
Code:
Sub test()
    GetValuesFromAClosedWorkbook "C:", "Book1.xls", _
            "Sheet1", "A1:K30"
End Sub
Sub GetValuesFromAClosedWorkbook(fPath As String, _
            fName As String, sName, cellRange As String)
    With ActiveSheet.Range(cellRange)
        .FormulaArray = "='" & fPath & "\[" & fName & "]" _
                    & sName & "'!" & cellRange
        .Value = .Value
    End With
End Sub
I tried it and it does work, but you'll have to have a Book1.xls in C:\ and have data in Sheet1 range A1:K30
Hope that helps
 
Upvote 0
wut,

What is the directory/folder/fPath to the closed workbook?

What is the filename/fName of the closed workbook?

What is the sheetname/sName of the closed workbook, worksheet containing the range of data values to grad?

What is the range/cellRange of the closed workbook?

The cellRange in the closed workbook (in this case) must match the cellRange in the active/opend worksheet to copy into.




Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Explicit
Sub test()
GetValuesFromAClosedWorkbook "C:", "Book1.xls", "Sheet1", "A1:K30"
End Sub

Sub GetValuesFromAClosedWorkbook(fPath As String, fName As String, sName, cellRange As String)
With ActiveSheet.Range(cellRange)
  .FormulaArray = "='" & fPath & "\[" & fName & "]" & sName & "'!" & cellRange
  .Value = .Value
End With
End Sub


Then run the test macro.
 
Upvote 0

Forum statistics

Threads
1,224,591
Messages
6,179,768
Members
452,940
Latest member
rootytrip

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