Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88809

Get Average from multiple columns from .csv files and save it to a master sheet in excel using vba

$
0
0

Hi to all the community. My question is this: Is there a way to retrieve the average from multiple .csv files and paste it to a master sheet in excel using a macro without the need of opening the .csv?

I've tried this code:

Sub Button1_Click()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim file_text_tmp As String
Dim i As Integer

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("E:\Files")

field_num = 3
delimiter = ","

Col = 1
For Each objFile In objFolder.Files
   Cells(1, Col) = Val(Left(Right(objFile.Name, 11), 5))
   Open objFile.Path For Input As #1
     Row = 2
     Do While Not EOF(1)
        Line Input #1, file_text_tmp
        If field_num > 1 Then
           For k = 1 To field_num - 1
              file_text_tmp = Right(file_text_tmp, Len(file_text_tmp) - InStr(1, file_text_tmp, delimiter))
           Next k

        End If
        If InStr(1, file_text_tmp, delimiter) = 0 Then
           Cells(Row, Col) = file_text_tmp
           Col = Col + 1
        Else
           Cells(Row, Col) = Left(file_text_tmp, InStr(1, file_text_tmp, delimiter) - 1)
           Col = Col + 1
        End If
        Row = Row + 1
     Loop
   Close #1
   Col = Col + 1
Next objFile
'Rows("1:6").Delete
'Average Data
Row = ActiveSheet.UsedRange.Rows.Count + 1
For i = 1 To Col - 1
   With Cells(Row, i)
      .Value = Application.WorksheetFunction.Average(Range(Cells(2, i), Cells(Row - 1, i)))
      .Font.Bold = True
   End With
Next i

End Sub

But it only put all my data in one cell because my .csv archives have multiple columns separated by commas. Thanks and I really appreciate the help.

More details:

Each .csv has this>

enter image description here

and I have several files like these>

enter image description here

So I need to put a lot of those .csv files into a folder and then with a Macro calculate the average for each .csv and save it to a Master Excel. I'm stuck in the average x_x

In other words, I want to get the average of each column and save that line into a master spreadsheet. enter image description here


Viewing all articles
Browse latest Browse all 88809

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>