Excel VBA writing an carriage-return or empty row at the end when saving a text file

Tanquen

Board Regular
Joined
Dec 14, 2017
Messages
60
Office Version
  1. 365
Platform
  1. Windows
I'm creating a file with Excel VBA and everything is working but there is a empty line or carriage-return at the end I can't get rid of.

I am adding a carriage-return for each line when joining them back together but nothing I've tried will stop it from adding one at the end.

VBA Code:
' Join the lines back together
    modifiedContent = Join(lines, vbCrLf)

But even if I don't add any carriage-return there is still one at the end of the file.

Code:
' Join the lines back together
    modifiedContent = Join(lines)

This is the last row of a good file without the carriage-return or blank line at the end.
1722562738291.png


This is a bad file.
1722562773886.png


This is the script that writes the file.

Code:
    ' Join the lines back together
    modifiedContent = Join(lines, vbCrLf)


    ' Check if modifications were made
    If fileContent <> modifiedContent Then
        ' Create an instance of ADODB.Stream
        Set stream = CreateObject("ADODB.Stream")
    
        ' Specify the stream type (binary) and character set (UTF-8)
        stream.Type = 2 ' adTypeText
        stream.charset = "utf-8"
    
        ' Open the stream and write the content
        stream.Open
        stream.WriteText modifiedContent
    
        ' Save the content to the new file
        stream.SaveToFile newFilePath, 2 ' adSaveCreateOverWrite
    
        ' Close the stream
        stream.Close
    
        ' Clean up
        Set stream = Nothing
    
        MsgBox "The file has been successfully modified and saved as " & newFilePath
    Else
        MsgBox "No modifications were necessary."
    End If
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
This is common when stringing together values and separating them with delimiters such as a comma and/or spaces. (e.g. 1, 2, 3, 4, )
You get the length of the string str - in the above case, 12. Then take all of the left but minus 2 characters: str = Left(str,Len(str)-2)
For your case, not sure if the number to subtract will be 1 or 2. It could be 2 because typically a line wrap is comprised of Chr(13) & Chr(10). Since you are using vbCrLf then it should be 2.
But even if I don't add any carriage-return there is still one at the end of the file.
That I can't explain, unless it's coming from another source. If my first suggestion doesn't work then I'd try retrieving the last 2 or so characters only, but one at a time to see what they are by using Asc() function on the result.
 
Upvote 0
but there is a empty line or carriage-return at the end I can't get rid of.

I am adding a carriage-return for each line when joining them back together but nothing I've tried will stop it from adding one at the end.

Is the last element of the lines array empty?
 
Upvote 0
Using Notepad++ with the View→Show Symbol→Show All Characters option it shows that CR has been added at the end. It happens each time the file is processed. So if I open it again and save it again there will be two CRs at the end.

Before:
1722613179532.png


After:
1722613038286.png


So I think it's the ADODB.Stream function that I need to use so I can process special characters.
 
Upvote 0
I had added this in the other day and I'm sure it did not work but today it is. ??? I had tried a few other things at the time that I've now removed, maybe they conflicted somehow.

Added in after the join.

VBA Code:
' Remove trailing empty lines and carriage returns
    lines = Split(modifiedContent, vbCrLf)

    ' Remove trailing empty lines
    Do While UBound(lines) >= 0 And Trim(lines(UBound(lines))) = ""
        ReDim Preserve lines(UBound(lines) - 1)
    Loop

    ' Rejoin the lines into a single string
    modifiedContent = Join(lines, vbCrLf)
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top