CELLS INTO TEXT FILE with VBA

R

RealInfo

Hi all

How can I turn the content of a series of cells into a string that will be
used at VBA writefile command .

The Idea is to generate a text file for a series of cells in excel with VBA.

Thanks

EC
 
R

rspowell

Hi all

How can I turn the content of a series of cells into a string that will be
used at VBA writefile command .

The Idea is to generate a text file for a series of cells in excel with VBA.

Thanks

EC


Here is a snippet of VBA code to provide you with an example

-- you will need to modify this just a bit for your application ...


' Initialize a variable for the text file path
FilePath$ = ThisWorkbook.Path & "\" & _
"FileName.txt"

' Get a free file number
FileNum& = FreeFile

' Open file for processing
Open FilePath$ For Output As #FileNum&

With Sheet1

' Loop through the rows
For r& = 1 To LastRow&

' Clear the string
ThisLine$ = ""

' Loop across the columns with the content to be appended to
the string
For c& = 1 To LastCol&

' Concatenate the contents of each cell across the row
ThisLine$ = ThisLine$ & Trim$(.Cells(r&, c&).Value)

' Apply a delimeter character between the fields
If c& < LastCol& Then ThisLine$ = ThisLine$ &
DELIMITER_CHAR$

Next c&

' Write the string
Print #FileNum&, ThisLine$

Next r&

End With

' Close the file
Close #FileNum&


Hope it helps you !

- Rodney POWELL
Microsoft MVP - Excel

www.BeyondTechnology.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top