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
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