0 votes
in VBA by Expert (892 points)

Hello everyone

I have a simple issue. The following UDF I am using to export array of values to text file .. It is working well

Function ArrayToTextFile(a As Variant, strPath As String)
    Dim fso As Object, fn As Object, i As Long

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fn = fso.OpenTextFile(strPath, 2, True)

    For i = LBound(a) To UBound(a)
        fn.WriteLine a(i)
    Next i

End Function

But I have noticed at the end of the text file ouput a new empty line. How can I get rid of it?

1 Answer

+1 vote
by Expert (892 points)
selected by
Best answer


I could figure it out by using Write method instead

    For i = LBound(a) To UBound(a)
        fn.Write a(i)
        If i <> UBound(a) Then fn.Write vbCrLf
    Next i


Welcome to wellsr Q&A
wellsr Q&A is the VBA and Python programming community that rewards you for learning how to code.

Getting Started
VBA Cheat Sheets (On Sale Now)

Earn free prizes for asking VBA and Python questions and for answering questions asked by others in our community.

Looking for something else? Hire our professional VBA Help, instead.

What makes us different?
Our points system rewards you with a chance for free gifts based on the quality of your questions and answers. All you have to do is post and you could get rewarded, like these members:

ParserMonster $25 Amazon Gift Card
Hightree $10 Amazon Gift Card
Thales1 $10 Amazon Gift Card
runfunke $10 Amazon Gift Card
coolag $10 Amazon Gift Card
Siew Hun $10 Amazon Gift Card

So, why don't you join us? It really is an encouraging way to motivate members in our VBA and Python community.


For more programming tips visit the VBA Tutorials Blog and the Python Tutorials Blog.