Hello, I am new to using the macro function in excel, and would greatly appreciate help creating a function to batch process my data. I have a directory of text files which I would like to import into excel in order, and then to automatically produce the average and mid point for each column (for each file individually). I have attached the script for running two files individually (another tricky bit is to note that the files all have the same columns, but are variable in rows). I don't know where to begin to get it to batch process the thousands of files I have.
I greatly appreciate your help!
Sub Macro_example()
'
' Macro_example Macro
' This shows an example of the steps that I need a macro to complete. 2 files are processed here, and I have a directory of thousands of text files labelled numerically (label is the same as input to column A) I would like to automatically import.
'
' Keyboard Shortcut: Ctrl+Shift+B
'
Range("A2").Select
ActiveCell.FormulaR1C1 = "1"
Range("B2").Select
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Users\Public.Stansbury\Desktop\Formant vocal learning\Praat outputs\1." _
, Destination:=Range("$B$2"))
.Name = "1._1"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 2
.TextFileParseType = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1)
.TextFileFixedColumnWidths = Array(8, 16, 13, 14)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Range("G2").Select
ActiveCell.FormulaR1C1 = "=AVERAGE(RC[-5]:R[26]C[-5])"
Range("G2").Select
Selection.AutoFill Destination:=Range("G2:K2"), Type:=xlFillDefault
Range("G2:K2").Select
Range("B15:F15").Select
Selection.Copy
Range("L2").Select
ActiveSheet.Paste
ActiveWindow.ScrollRow = 2
ActiveWindow.ScrollRow = 3
ActiveWindow.ScrollRow = 4
ActiveWindow.ScrollRow = 5
ActiveWindow.ScrollRow = 6
ActiveWindow.ScrollRow = 7
ActiveWindow.ScrollRow = 8
ActiveWindow.ScrollRow = 9
ActiveWindow.ScrollRow = 10
ActiveWindow.ScrollRow = 11
ActiveWindow.ScrollRow = 12
ActiveWindow.ScrollRow = 13
ActiveWindow.ScrollRow = 14
ActiveWindow.ScrollRow = 15
ActiveWindow.ScrollRow = 16
Range("A29").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "2"
Range("B29").Select
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Users\Public.Stansbury\Desktop\Formant vocal learning\Praat outputs\2." _
, Destination:=Range("$B$29"))
.Name = "2._1"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 2
.TextFileParseType = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1)
.TextFileFixedColumnWidths = Array(8, 16, 12, 14)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Range("G29").Select
ActiveCell.FormulaR1C1 = "=AVERAGE(RC[-5]:R[11]C[-5])"
Range("G29").Select
Selection.AutoFill Destination:=Range("G29:K29"), Type:=xlFillDefault
Range("G29:K29").Select
Range("B34:F34").Select
Selection.Copy
Range("L29").Select
ActiveSheet.Paste
ActiveWindow.ScrollRow = 24
ActiveWindow.ScrollRow = 23
ActiveWindow.ScrollRow = 21
ActiveWindow.ScrollRow = 19
ActiveWindow.ScrollRow = 17
ActiveWindow.ScrollRow = 16
ActiveWindow.ScrollRow = 15
ActiveWindow.ScrollRow = 14
ActiveWindow.ScrollRow = 13
ActiveWindow.ScrollRow = 12
ActiveWindow.ScrollRow = 11
ActiveWindow.ScrollRow = 9
ActiveWindow.ScrollRow = 8
ActiveWindow.ScrollRow = 6
ActiveWindow.ScrollRow = 4
ActiveWindow.ScrollRow = 1
End Sub
Bookmarks