How to populate a .CSV file using ADO

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
About two-fifths of the way down that page it says:-
Note You'll notice that we keeping talking about reading text files, and never say anything about writing to text files. Why? Well, unfortunately, the ODBC Text Driver (which is what we're using here) can only read from files; it can't write to them. To write to a text file, you'll have to use the FileSystemObject or something similar.
 
Upvote 0
I totally missed that, thanks. From VBA help under FileSystemObject Object:

Code:
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("c:\testfile.txt", True)
a.WriteLine("This is a test.")
a.Close

How would I do this with tens of thousands of rows of data? Would it be possible to just insert an entire range all at once?

It's really fast returning values from a csv file or saving as one... I'm hoping writing to one will be just as quick.
 
Upvote 0
It'll be slower as it'll have to loop through the rows doing a WriteLine for each one.

Can you not do this:-
Code:
ThisWorkbook.SaveAs Filename:="whatever.csv", FileFormat:=xlCSV
Or do you need to export only certain parts of the worksheet?
 
Upvote 0
I need to turn sheets from multiple closed workbooks into .CSV files. The key being, without opening them. I haven't found anything yet that looks remotely promising. Do you have a clue what I might do?
 
Upvote 0
I should have said:

I need to turn sheets from multiple closed workbooks into a single .CSV file, without opening the .xls files.

Rich (BB code):
Open MyCSV.csv For Append As #1
 
For i1 = 1 to LastUsedRow
 
    For i2 = 1 to LastUsedColumn
 
        Cells(r, c) = a
 
        FileSystemObject.Write a & ","
 
    Next
 
Next

Something like that, maybe? Except, is there a way to write a whole row or range at once, instead of just a single cell's value?
 
Upvote 0
I don't know how to read the contents of an Excel workbook without opening it.
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,678
Members
452,937
Latest member
Bhg1984

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top