Get Range from Closed Workbook, faster way to do?

TTom

Well-known Member
Joined
Jan 19, 2005
Messages
518
I found the following code to get values from a closed workbook.
The problem is this is rather slow if the range of cells is large since it collects data cell by cell and not as a range.
Lets say I am getting values for Range(A1:E100), the function and proceedure go through 500 cells one at a time to get data, vs. one step to get entire range all at once.
Make sense?

***

Function GetValue(path, file, sheet, ref)

' Retrieves a value from a closed workbook
Dim arg As String

' Make sure the file exists
If Right(path, 1) <> "\" Then path = path & "\"
If Dir(path & file) = "" Then
GetValue = "File Not Found"
Exit Function
End If

' Create the argument
arg = "'" & path & "[" & file & "]" & sheet & "'!" & _
Range(ref).Range("A1").Address(, , xlR1C1)

' Execute an XLM macro
GetValue = ExecuteExcel4Macro(arg)

End Function
Sub Import_From_Closed_Workbook

f = "FileToBeImportedFrom.xls"
p = "C:\ImportFromFolder"
s = "Sheet1" 'enables to specify which sheet
a = "A1:E100" 'range to be imported

NumberOfRows = Range(a).Rows.Count
NumberOfColumns = Range(a).Columns.Count

For r = 1 To NumberOfRows
For c = 1 To NumberOfColumns
b = Range(a).Cells(r, c).Address
Range("a2").Cells(r, c) = GetValue(p, f, s, b)
Next c
Next r

End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
So, are you getting values from a closed workbook dynamically? I just want to know the reasoning behind not using direct links in formulae, that's all.
 
Upvote 0
I've recommended this code (Nimrod) before :) :

Code:
Sub test() 
GetValuesFromAClosedWorkbook "C:", "Book1.xls", _ 
"Sheet1", "A1:A30" 
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
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,289
Members
452,902
Latest member
Knuddeluff

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