Remove last empty row in a textfile

karpe86

New Member
Joined
Dec 2, 2016
Messages
9
I am using a macro to copy cells from an Excel worksheet to a new text file. The last row of the text file is empty and I can't remove this. The code looks like this:

Dim r As Long
Dim arr As Variant
Dim file As Variant
Set file = CreateObject("Scripting.FileSystemObject").CreateTextFile("c:\temp\returförslag" + " test.exp", True, True)
With ActiveSheet
arr = .Range(.[A1], .UsedRange.Cells(.UsedRange.Cells.Count))
For r = 1 To UBound(arr)
strfile = Join(WorksheetFunction.Index(arr, r), vbTab)
strfile = Replace(strfile, Chr(13), "")
strfile = Replace(strfile, Chr(10), "")
strfile = Replace(strfile, vbcrlf"")
'file.WriteLine Join(WorksheetFunction.Index(arr, r), vbTab)
file.WriteLine strfile
Next

End With

I have tried to use the replace function to remove the carriage return but wunfortunately with no success. Can you please help me out with this annoying problem?
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Welcome to the forum!

A blank line at the end is the usual feature of most csv and txt delimited files. I could show you how to remove it. First though, you might want to try Chip's routine and see how it works out for you. You will probably want to add .Select to the range set for arr and use Chip's option to use selected range. Exporting And Importing Text Into Excel
 
Upvote 0
Thanks for your assistance. I have looked into Chip's routine but I don't find the solution. If you can help me I would be more than happy!

Welcome to the forum!

A blank line at the end is the usual feature of most csv and txt delimited files. I could show you how to remove it. First though, you might want to try Chip's routine and see how it works out for you. You will probably want to add .Select to the range set for arr and use Chip's option to use selected range. Exporting And Importing Text Into Excel
 
Upvote 0
Thanks for your assistance. I have looked into Chip's routine but I don't find the solution. If you can help me I would be more than happy!

I am sorry to bump this thread but is there anyone who can help me out with this?
 
Upvote 0
Try: arr = .Range("A1").CurrentRegion.Value
 
Upvote 0
I tried to replace arr = .Range(.[A1], .UsedRange.Cells(.UsedRange.Cells.Count)) with arr = .Range("A1").CurrentRegion.Value but the last carriage return in the text file remain.
 
Upvote 0
I found the solution by using command "write" instead of "writeline" when creating the last row in the textfile.
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,426
Members
448,961
Latest member
nzskater

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