how to get rid of "^M" in the output file

G

gordian

I'm new to Excel vba programming, so I'm not sure if I'm doing the
right thing.

In my macro, if I do:

''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Dim fileName As String
Dim colIndex As Integer
Dim rwIndex As Integer

fileName = "test.txt"
colIndex = 4

Open fileName For Output As #1

For rwIndex = 2 To 10
Print #1, Cells(rwIndex, colIndex).Value
Next rwIndex

close #1

''''''''''''''''''''''''''''''''''''''''''''''''''''''''

What I got in the output file were lines that ended with "^M". How can
I remove these characters?

Thanks
 
N

NickHK

Nothing in your code to indicate these "^M" chars were added, so I can only
assume they are in the cell values.
Check the source data.

Also, check the help for "FreeFile".

NickHK
 
D

Dave Peterson

I bet that Gordian means that ^M is the carriage return character
(vbcr=chr(13)).

It's for end of line (along with vblf=(chr(10)) in DOS files.

But I'm not sure what the OP really wants.

A one line text file?

Print #1, Cells(rwIndex, colIndex).Value;
or
Print #1, Cells(rwIndex, colIndex).Value & " ";

or what???

To the OP:

Maybe it's just your text editor that's giving you the wrong impression. Try
opening the text file in Notepad. Does it look ok?
 
N

NickHK

Dave,
I see what you mean. If the OP is opening the output file in a different
environment that does not expect the normal end of line combination, then
problems will arise.
As you say, depends what the aim is .

NickHK
 

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