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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting duplicates in Excel - whats wrong with this code please ? 1

Status
Not open for further replies.

psbrown

MIS
Oct 16, 2001
40
Hi I am new to VBA but have a done other languages in the past. I want to remove duplicates in column p on an Excel worksheet.

I have developled the code below to loop through the column.

my test data is 2 rows with the value of 2-2005(one copied from the other)

my problem is that my the line below marked********
if target = subject does not seem to find the equivlence and runs the msgbox ("else") statemen even though the msgbox's above show correct values for subjct and target.

What is wrong with this statment please I am stuck?

Sub DupDelete()
Dim mark As Integer
Dim Target As String
Dim Subject As String
Dim PNoRows As Integer

'Count No Rows in Column P
With Worksheets("SFS")
PNoRows = .Cells(.Rows.Count, "P").End(xlUp).Row 'count number of rows
End With

'Loop though Column check each cell against cells below and overwrite any duplicates with blank
For i = 1 To PNoRows
Target = Worksheets("SFS").Cells(i, "p").Value
mark = i + 1
For m = mark To PNoRows
MsgBox ("mark = " & mark)
Subject = Worksheets("SFS").Cells(m, "p").Value
MsgBox ("Subject =" & Subject)
MsgBox ("target =" & Target)
********If Taget = Subject Then
Worksheets("SFS").Cells(m, "p").Value = "---"
MsgBox ("swapping")
Else
MsgBox ("else")
End If
Next
Next

End Sub
 
Replace Taget with Target
Tip: use the OPTION EXPLICIT instruction.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Is it because it is called Taget ?

Try using option explicit.

Hope this helps

Chris
 
Thanks for That PVH, because I am a rookie I automatically thought it was due to bad logic not spelling.

Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top