Put the code written into the function, replacing the static file name with a variable, then call this function for each *.csv file in the folder. To get the example below, you need to save the file with this macro in the same folder as the csv files. For my quick test, I had to replace the delimiter from ; before , and delete the last line .UseListObject = False .
Sub ImportAllCSV() Dim FName As Variant, R As Long R = 1 FName = Dir("*.csv") Do While FName <> "" ImportCsvFile FName, ActiveSheet.Cells(R, 1) R = ActiveSheet.UsedRange.Rows.Count + 1 FName = Dir Loop End Sub Sub ImportCsvFile(FileName As Variant, Position As Range) With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;" & FileName _ , Destination:=Position) .Name = Replace(FileName, ".csv", "") .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .TextFilePromptOnRefresh = False .TextFilePlatform = xlMacintosh .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = True .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = False .TextFileSpaceDelimiter = False .TextFileOtherDelimiter = "," .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) .Refresh BackgroundQuery:=False End With End Sub
stenci
source share