Delete Rows

Onjslnjps

Spammer
Joined
Jul 1, 2011
Messages
10
Hi,

I have a series of items in different rows and what I need to do is sort them so that there are no duplicates.

So for example:
items origin
row 1 apples france
row 2 pears italy
row 3 bananas costa rica
row 4 pears italy

I need a macro that looks at first item in column and looks if this is repeated in any other row below. If it is repeated it should delete the row which is a duplicate. It should then move to next row and do the same for next item so that in the end all rows will have unique items.

Let's suppose I have four rows range A3:C6

How do I do a macro that leaves rows below?

row 1 apples france
row 2 pears italy
row 3 bananas costa rica

Thanks
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi

Without VBA, you could try:

Enter into cell D7:

=IF(ISNA(MATCH(C7,C$6:C6,0)),"","Delete")

Copy down, filter column D for "Delete", delete these rows
 
Upvote 0
If your using 2007 or later, you can simply use Data / Remove duplicates

For earlier versions try
Code:
Sub DelDups() 'removes duplicate cells from a selected range
Dim rngSrc As Range
Dim NumRows As Integer
Dim ThisRow As Integer
Dim ThatRow As Integer
Dim ThisCol As Integer
Dim J As Integer, K As Integer
Application.ScreenUpdating = False
Set rngSrc = ActiveSheet.Range(ActiveWindow.Selection.Address)
NumRows = rngSrc.Rows.Count
ThisRow = rngSrc.Row
ThatRow = ThisRow + NumRows - 1
ThisCol = rngSrc.Column
'Start wiping out duplicates
For J = ThisRow To (ThatRow - 1)
If Cells(J, ThisCol) > "" Then
For K = (J + 1) To ThatRow
If Cells(J, ThisCol) = Cells(K, ThisCol) Then
Cells(K, ThisCol) = ""
End If
Next K
End If
Next J
'Remove cells that are empty
For J = ThatRow To ThisRow Step -1
If Cells(J, ThisCol) = "" Then
Cells(J, ThisCol).Delete xlShiftUp
End If
Next J
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,557
Messages
6,179,510
Members
452,918
Latest member
Davion615

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