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
78
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

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
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,224,463
Messages
6,178,818
Members
452,881
Latest member
motivationgyan

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