G
Guest
Hello all-
I have an opened workbook that needs to pull data from a closed workbook.
Does anyone have a macro that can pull data from the closed workbook and
import it into the opened workbook? I don't know how many rows of data
there are, but there are always 4 columns. I am currently using the code
below, but it only pulls 100 rows of data. I need a macro that will pull
tdata from the last used row. Can any tweak the code or maybe have a better
one? Thanks
Sub Retrieve_Info()
P = "C:\Documents and Settings\David Truong\Desktop"
f = "Book2.xlsm"
s = "Sheet1"
Application.ScreenUpdating = False
For r = 1 To 100
For c = 1 To 3
a = Cells(r, c).Address
Cells(r, c) = GetValue(P, f, s, a)
Next c
Next r
Application.ScreenUpdating = True
End Sub
Private 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
Exit Function
End If
' Create the argument
arg = "'" & path & "[" & file & "]" & sheet & "'!" & _
Range(ref).Range("a2").Address(, , xlR1C1)
' Execute an XLM macro
GetValue = ExecuteExcel4Macro(arg)
End Function
I have an opened workbook that needs to pull data from a closed workbook.
Does anyone have a macro that can pull data from the closed workbook and
import it into the opened workbook? I don't know how many rows of data
there are, but there are always 4 columns. I am currently using the code
below, but it only pulls 100 rows of data. I need a macro that will pull
tdata from the last used row. Can any tweak the code or maybe have a better
one? Thanks
Sub Retrieve_Info()
P = "C:\Documents and Settings\David Truong\Desktop"
f = "Book2.xlsm"
s = "Sheet1"
Application.ScreenUpdating = False
For r = 1 To 100
For c = 1 To 3
a = Cells(r, c).Address
Cells(r, c) = GetValue(P, f, s, a)
Next c
Next r
Application.ScreenUpdating = True
End Sub
Private 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
Exit Function
End If
' Create the argument
arg = "'" & path & "[" & file & "]" & sheet & "'!" & _
Range(ref).Range("a2").Address(, , xlR1C1)
' Execute an XLM macro
GetValue = ExecuteExcel4Macro(arg)
End Function