Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Duplicate Rows (yes I already searched for the answer on the fourms:)) 1

Status
Not open for further replies.

Omnicube

MIS
Nov 7, 2011
40
US
I coded the below script to have Excel exit a sub if it finds duplicates in a row. However, when it runs, it tells performs the else function for n times there is no duplicate.

What I thought it would do is scrub column F for duplicates and display the duplicate message then exit the sub.

If there are no dupes, I only want the else section performed one time (not every time there is not a duplicate).

Code:
Sub mcrCheckDup()

Dim r As Range
Dim n As Long
Dim RowCount As Long

Range("F2").Select
Set r = Range(Selection, Selection.End(xlDown))
For n = 1 To r.Rows.Count
    If r.Cells(n, 1) = r.Cells(n + 1, 1) Then
    MsgBox "Duplicate Data", [vbExclamation]
    Exit Sub
    Else: MsgBox "ok"
End If
Next n

End Sub

Any ideas?
 
Code:
Sub mcrCheckDup()

Dim r As Range
Dim n As Long
Dim RowCount As Long

Range("F2").Select
Set r = Range(Selection, Selection.End(xlDown))
For n = 1 To r.Rows.Count
    If r.Cells(n, 1) = r.Cells(n + 1, 1) Then
    MsgBox "Duplicate Data", [vbExclamation]
    Exit Sub
End If
Next n
    MsgBox "ok"
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top