Hello,
I want to modify the below vba code to do the following in my macro enabled excel file.
I want to sum all the values for the set range B12:F16 in page called "Summary" for all the excel files in "C:\Foldername" in to the set range B2:F6 in my macro enabled spreadsheet (i.e. all the B12 in all the excel files in "C:\Foldername" summed in to B2 in my macro enabled excel file).
How can I do this using this code?
Regards.
I want to modify the below vba code to do the following in my macro enabled excel file.
I want to sum all the values for the set range B12:F16 in page called "Summary" for all the excel files in "C:\Foldername" in to the set range B2:F6 in my macro enabled spreadsheet (i.e. all the B12 in all the excel files in "C:\Foldername" summed in to B2 in my macro enabled excel file).
How can I do this using this code?
Code:
Private Function GetInfoFromClosedFile(ByVal wbPath As String, _
wbName As String, wsName As String, cellRef As String) As Variant
Dim arg As String
GetInfoFromClosedFile = ""
If Right(wbPath, 1) <> "\" Then wbPath = wbPath & "\"
If Dir(wbPath & "\" & wbName) = "" Then Exit Function
arg = "'" & wbPath & "[" & wbName & "]" & _
wsName & "'!" & Range(cellRef).Address(True, True, xlR1C1)
On Error Resume Next
GetInfoFromClosedFile = ExecuteExcel4Macro(arg)
End Function
Private Sub Workbook_Open()
Dim FolderName As String, wbName As String, r As Long, cValue As Variant
Dim wbList() As String, wbCount As Integer, i As Integer
FolderName = "C:\Foldername"
' create list of workbooks in foldername
wbCount = 0
wbName = Dir(FolderName & "\" & "*.xlsx")
While wbName <> ""
wbCount = wbCount + 1
ReDim Preserve wbList(1 To wbCount)
wbList(wbCount) = wbName
wbName = Dir
Wend
If wbCount = 0 Then Exit Sub
' get values from each workbook
r = 0
Workbooks.Add
For i = 1 To wbCount
r = r + 1
cValue = GetInfoFromClosedFile(FolderName, wbList(i), "Sheet1", "B2")
Cells(r, 1).Formula = cValue
Next i
End Sub
Last edited: