Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / General Excel Questions / May 2008

Tip: Looking for answers? Try searching our database.

Delete Unnecessary Rows

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Steve - 27 May 2008 20:56 GMT
I am trying to "clean up" a data sheet so that I can run a drag down command.
The data looks like this:

Name        Type         Score
John          AM             4
John          AM             5
John          LI               3
John          RI               7
John          RI               7
John          RI               6
Dave          AM             4
George       AM             6
George       LI               5
George       RI              4

I would like to turn that into this

Name        Type          Score
John           AM            5
John           LI              3
John           RI             7
George       AM            6
George       LI              5
George       RI              4

So basically there are three types; AM, LI, RI and I want to take the
highest score for each type and delete any other score.  The catch is, if the
person doesn't all three have 3 types, then I have to delete them.  Can
anyone help me?
Dave Peterson - 28 May 2008 01:23 GMT
How about a macro?

If that's ok, try this against a copy of your data--or close the file without
saving.  The macro destroys a lot of data when it runs!

Option Explicit
Sub testme()

   Dim LastRow As Long
   Dim FirstRow As Long
   Dim iRow As Long
   Dim wks As Worksheet
   
   Set wks = Worksheets("Sheet1")
   
   With wks
       FirstRow = 2
       LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
       
       'sort by name (ascending)
       'then by type (ascending)
       'then by score (descending--highest score on top)
       With .Range(.Cells(FirstRow, "A"), .Cells(LastRow, "C"))
           .Cells.Sort Key1:=.Columns(1), Order1:=xlAscending, _
                       Key2:=.Columns(2), Order2:=xlAscending, _
                       Key3:=.Columns(3), Order3:=xlDescending, _
                       Header:=xlNo, OrderCustom:=1, _
                       MatchCase:=False, Orientation:=xlTopToBottom
       End With
       
       'remove duplicate rows based on name & type.  
       'we delete from the bottom up.  And since the scores are in
       'descending order, we get rid of the lower score rows
       For iRow = LastRow To FirstRow + 1 Step -1
           If .Cells(iRow, "A").Value = .Cells(iRow - 1, "A").Value Then
               If .Cells(iRow, "B").Value = .Cells(iRow - 1, "B").Value Then
                   'duplicate in both name and type
                   .Rows(iRow).Delete
               End If
           End If
       Next iRow
       
       'reset last row -- after deleting rows, it's changed
       LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
       
       'look for rows where the value in column A is used exactly 3 times
       'if it's different from 3, then delete the row.
       For iRow = LastRow To FirstRow Step -1
           If Application.CountIf(.Range("A:a"), _
                  .Cells(iRow, "A").Value) <> 3 Then
               .Rows(iRow).Delete
           End If
       Next iRow
           
   End With
       
End Sub

If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm

> I am trying to "clean up" a data sheet so that I can run a drag down command.
>  The data looks like this:
[quoted text clipped - 25 lines]
> person doesn't all three have 3 types, then I have to delete them.  Can
> anyone help me?

Signature

Dave Peterson

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.