Open Multiple Files in a Folder, List file name, and calculate Average of Column

jyurasek02

New Member
Joined
Mar 1, 2016
Messages
1
I have excel files that we will be getting every week, and they always have the data in column B that we need to average.

I need a macro that opens all the files in the current folder, lists the file name, and calculates the average of all numeric values of column B.

I'm very new to VBA, and I don't really know what I'm doing. I tried copying a few things from other scripts, but couldn't get it to work.

I've listed the code I have now, and can email you the data file we want to calc. The thought is that we would put the new data into a new folder each time we get it, copy this test file to run the script. Then just copy and paste the results into a master spreadsheet somewhere that we can manipulate and sort.

Private Sub FindAverages_Click()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer
Dim myAvg As Integer
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String

'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set objFolder = objFSO.GetFolder(Application.ActiveWorkbook.Path)
i = 1
'loops through each file in the directory and prints their names
For Each objFile In objFolder.Files
'print file name
Cells(i + 1, 1) = objFile.Name

Set wb = Workbooks.Open(fileName:=myPath & objFile)

myAvg = wb.WorksheetFunction.Average(B)

'print file path
Cells(i + 1, 2) = myAvg

wb.Close SaveChanges:=False

i = i + 1
Next objFile
End Sub
 

Some videos you may like

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

Watch MrExcel Video

Forum statistics

Threads
1,123,514
Messages
5,602,091
Members
414,501
Latest member
mdhaumyu

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
Top