'Option Explicit requires me to assign my Variables with 'Dim' statements.
'
Option Explicit
Sub SumA1s()
'These are the 'Variables' and 'type' I used in the macro:
' MyDir is a string variable for the directory/folder where the data files are stored
' FN is a string variable for the filenames
' MyTotal is used to sum the various A1's
' Its type is 'Double' to sum numbers with decimal places
'
Dim MyDir As String, FN As String, MyTotal As Double
'This turns off screen flicker, and allows the code to run faster
'
Application.ScreenUpdating = False
'********** Change the path to suit your environment **********
'MyDir = "C:\TestData\"
MyDir = "C:\FLDR 1\"
'We are searching for '*.xls' filenames in MyDir
'
FN = Dir(MyDir & "\*.xls")
'Loop in the directory/folder until there are no more '*.xls' files.
'
Do While FN <> ""
'Do not include this workbook in the search for '*.xls' files,
' if it is stored in the directory/folder where the data files are stored.
'
If FN <> ThisWorkbook.Name Then
'Open each '*.xls' file found
'
With Workbooks.Open(MyDir & FN)
'Using the first worksheet in each workbook, where the data files are stored,
' instead of the worksheet name, that could vary.
'
With .Sheets(1)
'Keep a running total of all range 'A1' values.
'
MyTotal = MyTotal + .Range("A1").Value
End With
'close the opened '*.xls' file that was found.
'
.Close False
End With
End If
'Get the next '*.xls' filename.
'
FN = Dir
'Loop to the 'Do While' statement for the next '*.xls' file to open.
'
Loop
'In the active workbook, active worksheet, put the running total in range A1.
'
Range("A1") = MyTotal
'Turn on screen updating.
'
Application.ScreenUpdating = False
'End the macro.
'
End Sub