Solved

Macro to delete repeated rows based on column data (First row to stay)

Posted on 2014-10-07
9
226 Views
Last Modified: 2014-10-10
Column A has repeated data but other corresponding columns have different data.
Thus I can't use excel unique values or remove duplicate values as the from column B to E (Row 2 to 10) the data is repeated.

I only need the first line of the repeating rows.
For example, column A has 5 repeated data (ie. Cell A2 to A6) and the corresponding columns B to E (ie cell B2 to E6) has different values. Desired result: Row 2 only. Row 3 to 6 should be deleted.

Do the same for rest of the data. The first line of the repeating row should stay while the remaining to be deleted.
0
Comment
Question by:ceneiqe
  • 4
  • 3
  • 2
9 Comments
 
LVL 25

Expert Comment

by:ProfessorJimJam
ID: 40367671
Perhaps if you could upload a dunny example?
0
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 450 total points
ID: 40367685
You CAN use Excel's Remove Duplicates function to achieve this.  Just select Column A in the filters for removal; only the first instance-row will be preserved.

See this example:
remove duplicates -single column selection
Regards,
-Glenn
0
 
LVL 25

Expert Comment

by:ProfessorJimJam
ID: 40367874
put below code on a module and then run it .  it will do the work for you.


Sub RemoveDuplicateRows()
    Application.ScreenUpdating = False
    Dim ColN As Long
    Dim MyS As Worksheet: Set MyS = ActiveSheet
    Dim MyR As Range: Set MyR = MyS.Cells(1, 1).CurrentRegion
    Dim NumCol As Long: NumCol = MyR.Columns.Count
    Dim MyArray As Variant: ReDim MyArray(0 To NumCol - 1)
    For ColN = 1 To NumCol
        MyArray(ColN - 1) = ColN
    Next
    MyR.RemoveDuplicates Columns:=(MyArray), Header:=xlYes
    Dim rowcount As Long, i As Long, j As Long, k As Boolean
    rowcount = MyR.Rows.Count
    For i = rowcount To 1 Step -1
        k = 0
        For j = 1 To NumCol
            If MyR.Value2(i, j) <> "" Then
                k = 1
                Exit For
            End If
        Next j
        If k = 0 Then
            MyR.Rows(i).Delete Shift:=xlUp
        End If
    Next i
    Application.ScreenUpdating = True
End Sub

Open in new window

0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40368837
ProfessorJimJam, in my test example (seen above), this code did not remove any rows at all. :-(

I think this is because I do not have duplicates across all columns in any row.   The questioner appears to only want rows removed if duplicates appear in the first column only.  The change to the code would be to replace lines 7:11 with
MyR.RemoveDuplicates Columns:=1, Header:=xlYes

Open in new window


I do like the extra code to remove blank rows as part of this; nice touch.

Regards,
-Glenn
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 25

Assisted Solution

by:ProfessorJimJam
ProfessorJimJam earned 50 total points
ID: 40370980
Thanks very much Glenn,

i did not catch the question correctly.   i changed the code as per your point and this is the code that ceneiqe should use.

points also to be given to you Glenn.



Sub RemoveDuplicateRows()
    Application.ScreenUpdating = False
    Dim ColN As Long
    Dim MyS As Worksheet: Set MyS = ActiveSheet
    Dim MyR As Range: Set MyR = MyS.Cells(1, 1).CurrentRegion
    Dim NumCol As Long: NumCol = MyR.Columns.Count
     MyR.RemoveDuplicates Columns:=1, Header:=xlYes
    Dim rowcount As Long, i As Long, j As Long, k As Boolean
    rowcount = MyR.Rows.Count
    For i = rowcount To 1 Step -1
        k = 0
        For j = 1 To NumCol
            If MyR.Value2(i, j) <> "" Then
                k = 1
                Exit For
            End If
        Next j
        If k = 0 Then
            MyR.Rows(i).Delete Shift:=xlUp
        End If
    Next i
    Application.ScreenUpdating = True
End Sub

Open in new window

0
 

Author Comment

by:ceneiqe
ID: 40372154
Thanks all.

Hi professorjimjam, your code took a while to execute - probably because I have large set of data.

1. There are total of 1077 rows, including header (actually more) but i have previously deleted some manually on my own. The result is there are only 3 rows left.

2. if I use the "Remove Duplicates" method as explained by glenn, the response time is very fast.
and i get the desired result - 191 unique rows (taking the first row of data for each repeating rows). Perhaps i did not select the right column when i tried on the "Remove Duplicates" function in the first place.
0
 

Author Closing Comment

by:ceneiqe
ID: 40372156
base on the results i received, i assigned the points accordingly.
Hope this is ok.
0
 
LVL 25

Expert Comment

by:ProfessorJimJam
ID: 40372449
good that it worked for you.

i think all points should have been given to the comment of Glenn.  because his solution worked for you.

but thanks anyways.  my aim in this forum is not really about points, but also to learn from others.

have a good day
0
 

Author Comment

by:ceneiqe
ID: 40372610
No worries, I gave points to you for effort n prompt reply- really appreciate it. I encounter some experts who just gave one liner or just expect you to know things but in the first place if I know I wouldn't have asked. My questions might seem silly but I admit I am no expert. Then again I always do my best to be clear in my questions. Which is also why Im paying a subscription for the services rendered by the experts and I expect answers.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now