Export a range to a text file

S

Scott

Hello need some advise on how to procede

I need to be able to create a text file containg some text as well as
data that is within a named range in excel and then some more text. I
can handle printing to the text files using cell values etc but am
unsure of the best way to print the ranges data. Is there a way or
procedure to just print the range as is in csv format? As well my range
will contain about 6 columns, each containg a number field (formatting
of decimal places is important, some have 2 dec some 3 etc) Also the
range has a max of 50 rows however will always contain lower rows of
data then that, I dont want blanks printed to the file if there is no
data. I dont know whats the best way to handle this, will each row have
to be printed individually to the file? or is there a way to use the
range name to do it in one step?

If I do have to print each row one by one, can someone help me with some
example code (or where to get it) on the best way to find out the number
of rows of data there is then print each row of data to a text file in
csv?

field1,f2.f3,f4,f5,f6

tia
 
E

Earl Kiosterud

Scott,

Open "Test.txt" For Output As #1
Print #1, "Hello"; ' this line writes one field
Print #1, Range("A1").Text; ' this line appends a field
Print #1, Range("A2").Text ' this line ends the record
Close #1

Note the semicolons at the end -- they cause the record to not be ended.
Without it, it writes CRLF after the field is written, ending the record.

Range("A1").Text writes the cell as formatted in the worksheet. Range can
be replaced with any object that returns a range (Cells(Row, Column),
Selection, Offset(R, C), etc).

You'll need to loop through your columns and rows and do the Print
statements.
 
S

Scott

Thank-you for the 411

So I assume then that there is just no quick way of exporting all the fields
at once by naming all the fields as a "named range" then just outputting that?
 
E

Earl Kiosterud

Scott,

You may be able to use the Text Write Program at www.tushar-mehta.com. It's
a workbook with a program that has the option to write only a selection to a
text file. Other options. It's not set up for automation, but it's easy
enough to use code to stuff your options onto its Setup sheet, then run it.
 

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