Solved

Delete Rows based on 2 columns

Posted on 2011-09-12
8
231 Views
Last Modified: 2012-05-12
I found this code on this site (I've modified),

If Column A is duplicated, delete only the rows with the word "No Constraint" in Column 13.

See attached file, the rows highlighted are the ones that should be deleted.
Sub DeleteDupRows()

Dim i
Dim J
Dim RowsA
Dim Val1
Dim Val2


    Application.ScreenUpdating = False
RowsA = Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To RowsA
    Val1 = Cells(i, 2)

    For J = i + 1 To RowsA
        Val2 = Cells(J, 2)
        If Val1 = Val2 And Cells(i, 13) = "No Constraints" Then
            Rows(J).EntireRow.Delete
        End If
    Next J
Next i
    Application.ScreenUpdating = True
End Sub

Open in new window

TEST-1.xls
0
Comment
Question by:mato01
  • 4
  • 3
8 Comments
 
LVL 16

Expert Comment

by:carsRST
ID: 36525432
Try this code in place of yours...

Sub HideDupRows()

Dim i
Dim J
Dim RowsA
Dim Val1
Dim Val2


    Application.ScreenUpdating = False
RowsA = Cells(Rows.Count, 2).End(xlUp).Row
For i = RowsA To 1 Step -1
    Val1 = Cells(i, 2)

    For J = RowsA To 1 Step -1
        Val2 = Cells(J, 2)
        If Val1 = Val2 And Cells(J, 10) = "No Constraints" Then
            Rows(J).EntireRow.Hidden = True
        End If
    Next J
Next i
    Application.ScreenUpdating = True
End Sub
0
 
LVL 14

Expert Comment

by:peetjh
ID: 36525510
See attached code
Sub RemoveDupRows()

Dim i
Dim lastrow
 Application.ScreenUpdating = False
lastrow = [b65536].End(xlUp).Row
For i = lastrow To 2 Step -1
    If Cells(i, "A").Value = Cells(i - 1, "A").Value Then
        If Cells(i, "J").Value = "No Constraints" Then
            Rows(i).Delete
        ElseIf Cells(i - 1, "J").Value = "No Constraints" Then
            Rows(i - 1).Delete
        End If
    End If
Next i
    Application.ScreenUpdating = True
End Sub

Open in new window

0
 

Author Comment

by:mato01
ID: 36527015
This works, except that if A is not a duplicate it deletes the row with "No Constraints" anyway.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 14

Expert Comment

by:peetjh
ID: 36527669
When I test it does work as it should when "A" is duplicated. When I tested with your file I go from 15 rows to 13 with only the two highlighted rows being removed. But if I put "No Constraints" in cell "J4" then row 4 will be removed as well. This is as you have asked for because "A4" & "A5" are the same even though "B4 & B5" are different groups. If we need to look at column B as well just let us know.
0
 

Author Comment

by:mato01
ID: 36527898
I need both conditions to be true for the row to delete.

1. Cell A needs to be a duplicate entry

2. Cell J needs to havew the word "No Constraint".  

What the code is doing in both sets of script is deleteing/hiding every row with the word "No Constraints", regardless of Cell A is a duplicate. See attached.  What's in yellow should delete or hide, and rows shaded in green should not delete or hide.  If you run the script you will see that it deletes all those rows.

TEST-1-2-.xls
0
 
LVL 14

Accepted Solution

by:
peetjh earned 250 total points
ID: 36528018
If you look at cell A7 & A8 they are the same so since J7 contains "No Constraints" it is deleted, same with A14 & A15. Will B always match for the records you want to remove? In your previous example the values in B were sorted or appeared to be and were not duplicated. Is it possible to sort them before checking the values of J or not?
0
 

Author Comment

by:mato01
ID: 36530657
Yes.  How can I add to below toI sort columns A thru M prior to running the remove dup.  If not, I can just run another macro prior to running Sub RemoveDupRows




Sub RemoveDupRows()

Dim i
Dim lastrow
 Application.ScreenUpdating = False
lastrow = [b65536].End(xlUp).Row
For i = lastrow To 2 Step -1
    If Cells(i, "A").Value = Cells(i - 1, "A").Value Then
        If Cells(i, "J").Value = "No Constraints" Then
            Rows(i).Delete
        ElseIf Cells(i - 1, "J").Value = "No Constraints" Then
            Rows(i - 1).Delete
        End If
    End If
Next i
    Application.ScreenUpdating = True
End Sub
0
 
LVL 14

Expert Comment

by:peetjh
ID: 36530843
You can add this code to the code above before the For i statement. I did not have time to test this code it was modified from another macro I have. If your sheet is not named Sheet1 then replace the name throughout this code. You can add or remove the number of sort keys easily.

    Cells.Select
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear 'Change Sheet1 to Sheet Name
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("B1:B" & lastrow), _ 'Change Sheet1 to Sheet Name
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A2:A" & lastrow), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A2:M" & lastrow)
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

785 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