0
votes

I'm looking for help with some VBA Excel code to do some merging of various sheets into a workbook and during the copy perform some worksheet functions (e.g.). I have dozens for *.csv files, in the same directory that have a single worksheet, with the same name as the file (the file is hyatt.csv and the sheet name is "hyatt"). I want to create a new sheet in the workbook that I'm in and copy a range of data (i.e. Range(A33:FA500) from those *.csv files to a new worksheet. If possible I'd also like to run some Application.WorksheetFunctions like Min, Max, Average, StDev on those ranges and write them to some new cells.

My code looks like this and copies files no problem, but the range looping with the sheet name has me stuck and I don't know if I can/should next the loop for the worksheet functions.

Sub ConsolidatedWorkbooks()
   Path = "C:\Users\emilo\Documents\airport\"
   Filename = Dir(Path & "*.csv")
   Do While Filename <> ""
       Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
       For Each Sheet In ActiveWorkbook.Sheets
           Sheet.Copy After:=ThisWorkbook.Sheets(1)
       Next Sheet
       Workbooks(Filename).Close
       Filename = Dir()
    Loop
End Sub

Separate code for functions

Sub TestAverage()
   Sheets("Sheet1").Range("b2") = Application.WorksheetFunction.Min(Sheets("worksheet").Range("b2:fa2"))
   Sheets("Sheet1").Range("c2") = Application.WorksheetFunction.Max(Sheets("worksheet").Range("b2:fa2"))
   Sheets("Sheet1").Range("d2") = Application.WorksheetFunction.Average(Sheets("worksheet").Range("b2:fa2"))
   Sheets("Sheet1").Range("e2") = Application.WorksheetFunction.StDev(Sheets("worksheet").Range("b2:fa2"))
End Sub
What does this mean? the range looping with the sheet name has me stuck and I don't know if I can/should next the loop for the worksheet functions. Please clarify or rewrite in your question - dbmitch