#1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 921
Default Append text

Hi,

I am writing a string to an existing csv file.
I have the csv file opened and I want to append text to the csv file. How
is this done

For i = 1 to 100
Write #1, Array(i) 'Write this to the end of the csv file
next i

The csv file is very large - around 100mb

Thanks for your help on this
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 3,365
Default Append text

Have you already written the section of code to open the .csv to be written
to? I'm thinking not?

Make a copy of the .csv file to test this with so that your original doesn't
get messed up if I steer you wrong. If I'm wrong, no harm done, if I'm
right, then you have copy with the data appended.

Sub AddToCSVFile()
Dim myFileNum As Integer
Dim LC As Integer ' same as your 'i'
Dim myArray(1 to 100) as Integer ' same as your Array() - alter as needed

'....your code to create and fill Array() here

myFileNum = Freefile()
'change the path and name below as required
Open "C:\My Documents\myCSVFile.csv" For Append As #myFileNum
For LC = LBound(myArray) to UBound(myArray)
Print #myFileNum, myArray(LC)
Next
Close #myFileNum
'... more code here perhaps
End Sub

That is going to simply append the contents of myArray() as added 'rows' to
the .csv file. If each element of myArray() holds multiple entries that need
to be written as column information then we have to know a little more about
the array and what's in it.

But assume it is a 2 dimensional array as
Dim myArray(1 to 100,1 to 5) As String
then you could write the loop this way
Dim RC as Integer ' row counter for loop
Dim CC as Integer ' column counter for loop

For RC=1 to 100
For CC = 1 to 4 ' yes, 4 not 5 - you'll see
Print #myFileNum myArray(RC,CC) & ","; ' note the semi-colon use
Next ' ends CC loop
Print #myFileNum myArray(RC,5) ' write last element and terminate 'row'
Next ' ends RC loop

Finally, if the data in the array contains text and that text may contain
the separator character (comma) then it needs to be written this way:

For RC=1 to 100
For CC = 1 to 4 ' yes, 4 not 5 - you'll see
Print #myFileNum Chr(34) & myArray(RC,CC) & Chr(34) & ","; ' note the
semi-colon use
Next ' ends CC loop
Print #myFileNum Chr(34) & myArray(RC,5) & Chr(34) ' write last element and
terminate 'row'
Next ' ends RC loop




"Jeff" wrote:

Hi,

I am writing a string to an existing csv file.
I have the csv file opened and I want to append text to the csv file. How
is this done

For i = 1 to 100
Write #1, Array(i) 'Write this to the end of the csv file
next i

The csv file is very large - around 100mb

Thanks for your help on this

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I append same text to all the cells in a column at once? Abhijit Bamishte Excel Worksheet Functions 4 December 15th 06 07:26 PM
How can you append text in multiple cells in excel? Shane New Users to Excel 4 October 19th 05 10:49 PM
Append worksheets together in one bioyyy Excel Discussion (Misc queries) 1 October 14th 05 07:10 AM
Append Text to Cell Values Using Replace Ngan Excel Discussion (Misc queries) 4 June 4th 05 08:30 PM
How do I append imported text down an Excel Spreadsheet? Jdicicco Excel Worksheet Functions 1 February 17th 05 01:02 PM


All times are GMT +1. The time now is 06:40 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"