Reference a Range in a closed workbook in VBA

axg275

Active Member
Joined
Oct 5, 2004
Messages
308
Hello,

Do you guys know how I can reference range in a closed workbook on my network drive?

Thanks

Alex
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
This might be what you are looking for
Code:
'=============================================================================
'- FUNCTION TO GET VALUES FROM A CLOSED WORKBOOK
'- USES ExecuteExcel4Macro
'- Brian Baulsom May 2005
'=============================================================================
Dim MyPath, MyFile, MySheet, MyCell     ' worksheet
Dim R, C                                ' row, column
'=============================================================================
'- MACRO 1 : SINGLE CELL
'=============================================================================
Sub TestGetValue1()
    MyPath = "F:\"               ' path
    MyFile = "TestData2.xls"     ' file
    MySheet = "Sheet1"           ' sheet
    MyCell = "A1"                ' range
    '------------------------------------------------------------------------
    MsgBox GetValue(MyPath, MyFile, MySheet, MyCell)
    '------------------------------------------------------------------------
End Sub
'------- end of sub 1 -------------------------------------------------------
'=============================================================================
'- MACRO 2 : MULTIPLE CELLS (90 rows, 6 columns)
'=============================================================================
Sub TestGetValue2()
    MyPath = "F:\"               ' path
    MyFile = "TestData2.xls"     ' file
    MySheet = "Sheet1"           ' sheet
    '------------------------------------------------------------------------
    Application.ScreenUpdating = False
    With Worksheets("Sheet1")        '- TARGET WORKSHEET
        '--------------------------------------------------------------------
        '- LOOP CELLS BY ROW & COLUMN
        For R = 1 To 90
            For C = 1 To 6
                MyCell = Cells(R, C).Address
                .Cells(R, C) = GetValue(MyPath, MyFile, MySheet, MyCell)
            Next C
        Next R
        '--------------------------------------------------------------------
    End With
    '------------------------------------------------------------------------
    MsgBox ("Done")
    Application.ScreenUpdating = True
End Sub
'----------- end of sub 2 ----------------------------------------------------
'=============================================================================
'- FUNCTION
'=============================================================================
Private Function GetValue(Fpath, Ffile, Fsheet, Fref)
     Dim XL4macro As String
    '- Excel 4 macro string (requires R1C1 reference)
    XL4macro = "'" & Fpath & "[" & Ffile & "]" & Fsheet & "'!" & _
      Range(Fref).Address(ReferenceStyle:=xlR1C1)
    '-------------------------------------------------------------------------
    '-Run the macro
    GetValue = ExecuteExcel4Macro(XL4macro)
End Function
'-----------------------------------------------------------------------------
 
Last edited:
Upvote 0
Also, if relevant, the method Brian has used can be applied over ranges other than just a single cell. Will be much faster. Depends on what you are wanting.

There are also other ways such as with ADO that can work much more efficiently. Depends on what you are doing.

It is difficult to advise, Alex, without knowing what exactly is wanted. Regards, Fazza
 
Upvote 0
This might be what you are looking for
Code:
'=============================================================================
'- FUNCTION TO GET VALUES FROM A CLOSED WORKBOOK
'- USES ExecuteExcel4Macro
'- Brian Baulsom May 2005
'=============================================================================
Dim MyPath, MyFile, MySheet, MyCell     ' worksheet
Dim R, C                                ' row, column
'=============================================================================
'- MACRO 1 : SINGLE CELL
'=============================================================================
Sub TestGetValue1()
    MyPath = "F:\"               ' path
    MyFile = "TestData2.xls"     ' file
    MySheet = "Sheet1"           ' sheet
    MyCell = "A1"                ' range
    '------------------------------------------------------------------------
    MsgBox GetValue(MyPath, MyFile, MySheet, MyCell)
    '------------------------------------------------------------------------
End Sub
'------- end of sub 1 -------------------------------------------------------
'=============================================================================
'- MACRO 2 : MULTIPLE CELLS (90 rows, 6 columns)
'=============================================================================
Sub TestGetValue2()
    MyPath = "F:\"               ' path
    MyFile = "TestData2.xls"     ' file
    MySheet = "Sheet1"           ' sheet
    '------------------------------------------------------------------------
    Application.ScreenUpdating = False
    With Worksheets("Sheet1")        '- TARGET WORKSHEET
        '--------------------------------------------------------------------
        '- LOOP CELLS BY ROW & COLUMN
        For R = 1 To 90
            For C = 1 To 6
                MyCell = Cells(R, C).Address
                .Cells(R, C) = GetValue(MyPath, MyFile, MySheet, MyCell)
            Next C
        Next R
        '--------------------------------------------------------------------
    End With
    '------------------------------------------------------------------------
    MsgBox ("Done")
    Application.ScreenUpdating = True
End Sub
'----------- end of sub 2 ----------------------------------------------------
'=============================================================================
'- FUNCTION
'=============================================================================
Private Function GetValue(Fpath, Ffile, Fsheet, Fref)
     Dim XL4macro As String
    '- Excel 4 macro string (requires R1C1 reference)
    XL4macro = "'" & Fpath & "[" & Ffile & "]" & Fsheet & "'!" & _
      Range(Fref).Address(ReferenceStyle:=xlR1C1)
    '-------------------------------------------------------------------------
    '-Run the macro
    GetValue = ExecuteExcel4Macro(XL4macro)
End Function
'-----------------------------------------------------------------------------

Hi, I need code similar to TestGetValue2 but when vba run the function TestGetValue2, vba show error 9 subscript out of range

Does anyone know why and how to correct it?

Regards
 
Upvote 0

Forum statistics

Threads
1,221,600
Messages
6,160,733
Members
451,668
Latest member
alansbanans

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