mealypudding
New Member
- Joined
- Apr 4, 2011
- Messages
- 8
Hi there, with the help of you fabulous programmers I have made 2 macros which save me so much time at work. This next problem is a little out of my league tho. I have a closed workbook (with a different name each night) that I want to extra information from to a current open workbook. The information is totals of revenue that change rows but not columns.
So I'm trying to write a macro that finds these totals (by searching for a string eg "bar beer" then offsets the cell 2 right and extracts this value to different cells in my open workbook.
Ive got as far as extracting some cells from a defined workbook and inputting them into my current one but I need a bit more help.
Sub TestGetValue2()
p = "L:\"
f = "~DP570.xls"
s = "Sheet1"
Application.ScreenUpdating = False
For r = 1 To 100
For c = 1 To 12
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
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
thanks again :D
So I'm trying to write a macro that finds these totals (by searching for a string eg "bar beer" then offsets the cell 2 right and extracts this value to different cells in my open workbook.
Ive got as far as extracting some cells from a defined workbook and inputting them into my current one but I need a bit more help.
Sub TestGetValue2()
p = "L:\"
f = "~DP570.xls"
s = "Sheet1"
Application.ScreenUpdating = False
For r = 1 To 100
For c = 1 To 12
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
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
thanks again :D