I found a VBA code online to import data from CSV files and paste into the active Excel sheet. I already edited a few parts but can't get the last bit right.
What it currently does:
- Imports data (2nd row down) from all CSV files in same folder as main file (.xlsx) is stored
- Pastes data in active Excel sheet from row 2 down, all CSV files combined
What I still need is:
- Instead of pasting the data from Column A to ## I want to paste the data from Column B to ##, leaving Column A blank
- In Column A, I want the file name of the imported files, if for example the first file contains 10 data rows and the 2nd contains 20 data rows then the XLSX after running the macro will contain 30 data rows, the first column must show where the data originated from
- If possible I'd like to pick the files up from a subfolder, so the XLSX is not placed in the same folder as the imported CSV files
I'd be very greatful for anyone being able to help me with one or more of above improvements. Please find current code below.
Thank you
What it currently does:
- Imports data (2nd row down) from all CSV files in same folder as main file (.xlsx) is stored
- Pastes data in active Excel sheet from row 2 down, all CSV files combined
What I still need is:
- Instead of pasting the data from Column A to ## I want to paste the data from Column B to ##, leaving Column A blank
- In Column A, I want the file name of the imported files, if for example the first file contains 10 data rows and the 2nd contains 20 data rows then the XLSX after running the macro will contain 30 data rows, the first column must show where the data originated from
- If possible I'd like to pick the files up from a subfolder, so the XLSX is not placed in the same folder as the imported CSV files
I'd be very greatful for anyone being able to help me with one or more of above improvements. Please find current code below.
Thank you
Code:
Sub ImportAllCSV()
Dim FName As Variant, R As Long
R = ActiveSheet.UsedRange.Rows.Count + 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 = False
.TextFilePromptOnRefresh = False
.TextFilePlatform = xlMacintosh
.TextFileStartRow = 2
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.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