Assumptions:
- All text files are located in the same folder
- The text files are tab delimited
Use this Excel VBA code:
Sub tgr()
Dim oShell As Object
Dim oFSO As Object
Dim arrData(1 To 65000) As String
Dim strFolderPath As String
Dim strFileName As String
Dim strText As String
Dim DataIndex As Long
Dim lAvgLoc As Long
Set oShell = CreateObject("Shell.Application")
On Error Resume Next
strFolderPath = oShell.BrowseForFolder(0, "Select a Folder", 0).Self.Path & Application.PathSeparator
Set oShell = Nothing
On Error GoTo 0
If Len(strFolderPath) = 0 Then Exit Sub 'Pressed cancel
Set oFSO = CreateObject("Scripting.FileSystemObject")
strFileName = Dir(strFolderPath & "*.txt*")
Do While Len(strFileName) > 0
strText = oFSO.OpenTextFile(strFolderPath & strFileName).ReadAll
lAvgLoc = InStr(1, strText, "Daily Avg", vbTextCompare)
If lAvgLoc > 0 Then
strText = Mid(strText, lAvgLoc)
strText = Trim(Mid(Replace(strText, vbCrLf, String(255, " ")), Evaluate("MIN(FIND({1,2,3,4,5,6,7,8,9,0},""" & strText & """&1234567890))"), 240))
DataIndex = DataIndex + 1
arrData(DataIndex) = DateValue(Replace(strFileName, ".txt", vbNullString)) & vbTab & strText
End If
strFileName = Dir
Loop
If DataIndex > 0 Then
With Sheets.Add
.Range("A1:F1").Value = Array("DATE", "AVG1", "AVG2", "AVG3", "AVG4", "AVG5")
With .Range("A2").Resize(DataIndex)
.Value = Application.Transpose(arrData)
.TextToColumns .Cells, xlDelimited, xlTextQualifierDoubleQuote, Tab:=True
.NumberFormat = "mm-dd-yy"
End With
Application.DisplayAlerts = False
.SaveAs strFolderPath & "Daily Averages.csv", xlCSV
Application.DisplayAlerts = True
End With
End If
Set oFSO = Nothing
Erase arrData
End Sub
How to use a macro:
- Make a copy of the workbook the macro will be run on
- Always run new code on a workbook copy, just in case the code doesn't run smoothly
- This is especially true of any code that deletes anything
- In the copied workbook, press ALT+F11 to open the Visual Basic Editor
- Insert | Module Copy the provided code and paste into the module
- Close the Visual Basic Editor
- In Excel, press ALT+F8 to bring up the list of available macros to run
- Double-click the desired macro (I named this one tgr)