append to CSV

stuartford

Active Member
Joined
Nov 28, 2002
Messages
419
i have a CSV file with loads of data in it and also an excel sheets with the latest information.
i want to know if anyone knows a quick way to append the excel information onto the bottom of the CSV files data

hope this makes sense

cheers stu
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
How about something like this...

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> Append2CSV()
    <SPAN style="color:#00007F">Dim</SPAN> tmpCSV <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> <SPAN style="color:#007F00">'string to hold the CSV info</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> f <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>
    
    <SPAN style="color:#00007F">Const</SPAN> CSVFile <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> = "C:\VBA Code\test.csv" <SPAN style="color:#007F00">'replace with your filename</SPAN>
    
    f = FreeFile
    
    <SPAN style="color:#00007F">Open</SPAN> CSVFile <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Append</SPAN> <SPAN style="color:#00007F">As</SPAN> #f
        tmpCSV = Range2CSV(Range("A2:H3"))
        <SPAN style="color:#00007F">Print</SPAN> #f, tmpCSV
    <SPAN style="color:#00007F">Close</SPAN> #f
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#00007F">Function</SPAN> Range2CSV(list) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> tmp <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> cr <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> r <SPAN style="color:#00007F">As</SPAN> Range
    
    <SPAN style="color:#00007F">If</SPAN> TypeName(list) = "Range" <SPAN style="color:#00007F">Then</SPAN>
        cr = 1
        
        <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> r <SPAN style="color:#00007F">In</SPAN> list.Cells
            <SPAN style="color:#00007F">If</SPAN> r.Row = cr <SPAN style="color:#00007F">Then</SPAN>
                <SPAN style="color:#00007F">If</SPAN> tmp = vbNullString <SPAN style="color:#00007F">Then</SPAN>
                    tmp = r.Value
                <SPAN style="color:#00007F">Else</SPAN>
                    tmp = tmp & "," & r.Value
                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
            <SPAN style="color:#00007F">Else</SPAN>
                cr = cr + 1
                <SPAN style="color:#00007F">If</SPAN> tmp = vbNullString <SPAN style="color:#00007F">Then</SPAN>
                    tmp = r.Value
                <SPAN style="color:#00007F">Else</SPAN>
                    tmp = tmp & Chr(10) & r.Value
                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        <SPAN style="color:#00007F">Next</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
                    
    Range2CSV = tmp
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN>
</FONT>
 
Upvote 0
Been looking for something like this and I can confirm that it works, Awesome! So I will thank you on previous posters behalf.

Great job.
 
Upvote 0

Forum statistics

Threads
1,223,939
Messages
6,175,532
Members
452,651
Latest member
wordsearch

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