Need help with a Binary Excel file
Am I right in assuming that the data in those CSV files is a single line of values? If that's the case, I'd suggest splitting it into an array in your VBA code, instead of relying on formulas to do the splitting. Also, I don't see a reason why you're trying to import the data into this file and save it as an .xlsx file. The easier way is to just create an .xltx template that has all the column headers populated, add a new file based on that template, aggregate the data from those CSV files into that new workbook and save that as an .xlsx. Something like this:
Sub Import_CSV_Files()
'Turns off drawing the file contents to the screen, speeding up the process
Application.ScreenUpdating = False
'Create a new file to aggregate the contents of the CSV files
'First you need to create an Excel template file that contains the desired column headers
Dim targetFile As Workbook
Set targetFile = Workbooks.Add("C:\Example\template.xltx")
'Folder where all your CSV files are located
Dim filePath As String
filePath = "C:\Example\CSV files\"
'Fetching the first file in the folder specified above
Dim fileName As String
fileName = Dir(filePath)
'Loops through all the files in the folder specified above
Do While Len(fileName) > 0
Dim csvFile As Workbook
Set csvFile = Workbooks.Open(filePath & fileName)
'Load the data stored in the first cell into an array
'Assumes the data is separated by ;
'If not, change the delimiter to whatever type your files use
Dim data As Variant
data = Split(csvFile.Sheets(1).Range("A1").Value, ";")
csvFile.Close SaveChanges:=False
'Define the range in the target file where the stored data should be copied into.
'Range("A1048576") is the last possible row of an Excel sheet
'End(xlUp) jumps up from the last possible row to the last one filled with data
'Offset(1) moves one row below, to the first empty row in the file
'Resize(1, Ubound(data) + 1) expands the range to the number of columns determined by the copied data in the array
targetFile.Sheets(1).Range("A1048576").End(xlUp).Offset(1).Resize(1, UBound(data) + 1).Value = data
'Fetch the next file in the folder
fileName = Dir
Loop
targetFile.SaveAs fileName:="C:\Example\Output.xlsx", FileFormat:=xlOpenXMLWorkbook
targetFile.Close
Application.ScreenUpdating = True
End Sub
Also, on a more general note, when you write these routines, get into the habit of declaring your variables where you need them. You tend to have declaration blocks right at the top for everything, which, coupled with the terse names of your variables, makes it hard to understand what types they are and why you've used them. Especially since you rely too much on the object datatype, instead of using more appropriate ones. Like when you want to refer to a workbook, define the varible as a workbook. That way you also get the autocomplete feature for the methods of that type.

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now