remove duplicate lines in text file

yinkajewole

Active Member
Joined
Nov 23, 2018
Messages
281
how can i use a macro to remove duplicate lines in my text file such that the last one will remain, for instance
the boy is good
great thinking
great thinking
he lives
the boy is good
what's up
great thinking

it should now be like this
he lives
the boy is good
what's up
great thinking
 
Last edited:

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Not sure why that would be, unless you have some empty lines at the end of your input file. You can try changing this line:

Code:
If InStr(1, MyResult, vbCrLf & MyLines(i) & vbCrLf, vbTextCompare) = 0 Then
to
Code:
If InStr(1, MyResult, vbCrLf & MyLines(i) & vbCrLf, vbTextCompare) = 0 And MyLines(i) <> "" Then
If that doesn't work, let me know.
 
Upvote 0
If that doesn't work, let me know.[/QUOTE]
it worked partially.
Actually, the empty line used to be three lines there. The code I used to write the text file do add an empty line, while yours do add two, making three altogether.
So this code you gave me
Code:
If InStr(1, MyResult, vbCrLf & MyLines(i) & vbCrLf, vbTextCompare) = 0 And MyLines(i) <> "" Then
reduced the lines to two altogether.
 
Upvote 0
Change this line:

Code:
Print [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1"]#1[/URL] , Mid(MyResult, 2)

to

Code:
Print [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1"]#1[/URL] , Mid(MyResult, 2, Len(MyResult) - 2)
</pre>
 
Upvote 0
It's a bit tough to debug at a distance without seeing the file, but here's one last idea. Add one line before the Print #1 line, and change that line to:


Code:
MyResult = Replace(Replace(Trim(Replace(Replace(MyResult, " ", "|"), vbCrLf, " ")), " ", vbCrLf), "|", " ")
Print [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL]  , MyResult

I assume you're not using the "|" symbol anywhere in your file. If so, we can find something else.
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,307
Members
452,633
Latest member
DougMo

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