Delete double name

MJA

Board Regular
Joined
Feb 18, 2002
Messages
79
Hello Excel lovers,

I have a column (column A) with names.
When I add a name to the column, I play a macro that deletes the name if the name is already in the column!

But my solution doesn’t work.
When I play the macro it always deletes the last name!

Why?!
I think it goes wrong with the end(xldown) part.
When I play the macro step by step (with F8) the value of end(xldown) = 4121?!?!?!

Sub Test()
i = 1
Do Until Cells(i, 1).Value = ""
If Cells(1, 1).End(xlDown).Value = Cells(i, 1).Value Then
MsgBox "This name already exists!"
Cells(1, 1).End(xlDown).EntireRow.Delete
End If
i = i + 1
Loop
End Sub

Who can help me?
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi
I'm assuming that you are entering the new name on the next available row?
The value is finding it'self as a match and then deleting it'self.
Try this...

<pre>
Sub Test()
Dim c, LastRow
With ActiveSheet
LastRow = .UsedRange.Columns(1).Rows.Count
For Each c In .Range("a2:A" & LastRow)
If .Cells(LastRow, 1) = c.Value Then
If c.Row = LastRow Then Exit Sub
MsgBox "This name already exists!"
.Rows(LastRow).Delete
End If
Next
End With
End Sub

</pre>
Tom
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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