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

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

Kenneth Hobson

Well-known Member
Joined
Feb 6, 2007
Messages
3,179
Office Version
  1. 365
Platform
  1. Windows
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
 

karpe86

New Member
Joined
Dec 2, 2016
Messages
9
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
 

karpe86

New Member
Joined
Dec 2, 2016
Messages
9
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?
 

Sektor

Well-known Member
Joined
May 6, 2011
Messages
2,834

ADVERTISEMENT

Try: arr = .Range("A1").CurrentRegion.Value
 

karpe86

New Member
Joined
Dec 2, 2016
Messages
9
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.
 

karpe86

New Member
Joined
Dec 2, 2016
Messages
9
I found the solution by using command "write" instead of "writeline" when creating the last row in the textfile.
 

Watch MrExcel Video

Forum statistics

Threads
1,133,521
Messages
5,659,303
Members
418,496
Latest member
WHYCHumphrey

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
Top