Its been years since I’ve had to work in VBA and I’m super rusty. I’m trying to consolidate data from multiple csv files into a master workbook where each sheet matches up with each CSV file. The csvs are outputs from an analytics system. The names of the csvs will always be the same, they’re all in the same folder as the master workbook and are updated asynchronously.
There is a powerpoint linked to the workbook and the aim is to quickly update all the charts in the powerpoint based on the latest csv outputs. Ie open a csv, copy everything in there and paste it in the corresponding sheet of the same name overwriting what was there before.
I’m a little stuck! The code is opening and closing the CSVs, as well as clearing what’s already in each sheet but its not copying stuff over 🙁
Sub ImportCSVs()
'Summary: Import all CSV files from a folder into separate sheets
' named for the CSV filenames
Dim fPath As String
Dim fCSV As String
Dim wbCSV As Workbook
Dim wbMST As Workbook
Set wbMST = ThisWorkbook
fPath = (Application.ActiveWorkbook.Path & "") 'path to CSV files, assumes local path of master workbook
Application.ScreenUpdating = False
Application.DisplayAlerts = False
fCSV = Dir(fPath & "*.csv")
On Error Resume Next
Do While Len(fCSV) > 0
Set wbCSV = Workbooks.Open(fPath & fCSV) 'open a CSV file
wbMST.Sheets(ActiveSheet.Name).UsedRange.Clear
With wbMST.Sheets(ActiveSheet.Name).QueryTables.Add(Connection:="TEXT;" & fPath & fCSV, _
Destination:=wbMST.Sheets(ActiveSheet.Name).Range("A1"))
.TextFileParseType = xlDelimited
.TextFileCommaDelimiter = True
Columns.AutoFit
End With
wbCSV.Close
fCSV = Dir 'ready next CSV
Loop
Application.ScreenUpdating = True
Set wbCSV = Nothing
MsgBox "Import is complete"
End Sub
Any ideas on how to fix this?
Thanks so much, its been driving me nuts for the better part of 2 days.
See above notes – code is clearing sheets but not copying.
Calin Coman-Enescu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.