duplicate rows,delete last rows

d3j4vu

New Member
Joined
Dec 20, 2005
Messages
24
Hi, I'm a newbie with VBA. I am using Excel 2003.

The problem I am having is I want to delete the entire row when a duplicate is found. The duplicate is searched along column A.

I want to keep the first found row only and delete the last duplicate rows. Meaning, if for example there is the word "company" found in cells A2, A5 and A7, I want to delete rows 5 and 7, and keep row 2. The word can be any word, and I don't know what the word is beforehand, so I can't specify it in Macro.

I know there may be a lot of other people who asked similar questions. But I am truly a newbie, and I can't find the exact same question on the other posts.


------------------------------
In desperate need of help
:-(
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi - try this;

Code:
Sub test()
Dim myword As String
Dim c As Range
Dim myfound As Boolean
Dim myrow

myfound = False
myword = InputBox("Enter word to search for duplicates")
For Each c In Range("A1:A31")  ' change the range here  ***********
If c = myword And myfound = True Then
myrow = myrow & ", " & c.Row & ":" & c.Row
Else
If c = myword Then myfound = True
End If
Next
myrow = Right(myrow, Len(myrow) - 2)
Range(myrow).EntireRow.Delete
End Sub
 
Upvote 0
Hi, d3j4vu
Welcome to the Board !!!!!

assuming you want to remove any duplicates
try this
Code:
Option Explicit

Sub delete_duprows()
'Erik Van Geit
'051220
'checks column CC for dups
'dup rows except first appearance are deleted

'EXAMPLE
'checking column 1
'START WITH
'a2  b2  c2
'a3  b3  c3
'a2  b4  c4
'a5  b5  c5
'a3  b6  c6
'TO GET
'a2  b2  c2
'a3  b3  c3
'a5  b5  c5

Dim LR As Long
Dim Rng As Range

'EDIT these 2 lines
Const CC As Integer = 1 'check column
Const FR As Long = 2    'first row with data

LR = Cells(Rows.Count, 1).End(xlUp).Row
Set Rng = Range(Cells(FR, CC), Cells(LR, CC))

Application.ScreenUpdating = False
Columns(CC).Insert
    With Rng.Offset(0, -1)
    .FormulaR1C1 = "=IF(COUNTIF(R" & FR & "C[1]:RC[1],RC[1])=1,1,"""")"
    'to remove all duplicates, first appearance included
    '.FormulaR1C1 = "=IF(COUNTIF(R" & FR & "C[1]:R" & LR & "C[1],RC[1])=1,1,"""")"
    .Value = .Value
    On Error Resume Next
    .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    On Error GoTo 0
    End With
Columns(CC).Delete
Application.ScreenUpdating = True

End Sub
kind regards,
Erik
 
Upvote 0
Jimboy, thanx for the code. But the problem I have is I don't know what the duplicates will be. So it has to search for any duplicates and delete the duplicates and keep the original.

Erik, I copied and paste your code exactly to my macros. But it deleted all the info in column B and C as well. I don't know why it does that? I think it's because I have duplicates in column B and C. But I don't want it to look for duplicates in column B and C. I want it to look for duplicates only in column A, and leave any duplicates in other columns.

So for example, if I have
a2 b2 c2
a3 b3 c2
a2 b2 c7
a5 b3 c7
a3 b3 c7
I want TO GET
a2 b2 c2
a3 b3 c2
a5 b3 c7

Also, I am not too sure what to change in the code where you say
'EDIT these 2 lines
Const CC As Integer = 1 'check column
Const FR As Long = 2 'first row with data

As you can see I am completely clueless with macros, any help would be so appreciated.

-----------------
stupidity is a blessing?
 
Upvote 0
Forgot to say, if there is a blank cell in column A, I don't want to delete that row. I want to leave rows with blank cell alone.

Example

i have

"blank" b3 c3
"blank" b5 c3
a2 b5 c5
a3 b3 c2
a5 b3 c7
a2 b6 c7
a3 b3 c7
"blank" b5 c3

i want

"blank" b3 c3
"blank" b5 c3
a2 b5 c5
a3 b3 c2
a5 b3 c7
"blank" b5 c3


does that make any sense?


----------------------------------------
stupidity is definitely not a blessing
 
Upvote 0
lenze, thank you so so much.... i made some changes to the code and it did what i wanted to do!!!

thanx for everybody's effort too, erik and jimboy.
 
Upvote 0

Forum statistics

Threads
1,224,803
Messages
6,181,055
Members
453,015
Latest member
ZochSteveo

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