Macro to delete Row based on criteria?

mcintoshmc

Active Member
Joined
Aug 10, 2007
Messages
277
Currently I'm using this formula as a conditional format that highlights the entire row if the cell in column A is a duplicate.

Excel Formula:
=COUNTIF($A:$A,$A2)>1

I already have a macro that will delete the duplicates automatically, and have it as a button on my quick access toolbar.

I've created another forumula
Excel Formula:
=IF(NOT(OR($H2=List!$A$2,$H2=List!$A$3,$H2=List!$A$4)),"X","")
This will place a X in column P if a certain name is not located in column H.

Ideally, I would like a Macro that would delete the duplicate from column A that also has an X in column P.

This is the current macro I'm using to delete the duplicates

VBA Code:
Sub Delete_duplicate_rows()
Dim Rng As Range
Set Rng = Selection
Rng.RemoveDuplicates Columns:=Array(1), Header:=xlYes
End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Does that mean that you want to keep duplicates that don't have an X in column P?
 
Upvote 0
If it's purely a case of deleting all rows that have an "X" in column P, try the following on a copy of your workbook. Change the sheet name to suit.
VBA Code:
Option Explicit
Sub mcintosh()
    Application.ScreenUpdating = False
    Dim ws As Worksheet, i As Long
    Set ws = Worksheets("Sheet1")   '<~~ *** Change to actual sheet name ***
    i = WorksheetFunction.CountIf(ws.Range("P:P"), "X")
    If i > 0 Then
        ws.Range("A1").CurrentRegion.Sort Key1:=ws.Range("P2"), _
        order1:=xlAscending, Header:=xlGuess
        ws.Range("A2").Resize(i).EntireRow.Delete
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
I've come to the conclusion that I can't use the "X" because after the macro sorts and deletes, the formula for X will no longer be in all the required cells.

Perhaps deleting the row based on color will produce better results. So, now column H will contain the color, and those rows that contain green in the H cell will need to be deleted.

I'm using this macro, but doesn't work. Any thoughts?

VBA Code:
Sub deleterow()
     Dim myRange As Range
     Dim cell As Range
     
     Application.Calculation = xlCalculationManual
     Set myRange = Worksheets(1).Range("H2:H200")
  For i = myRange.Rows.Count To 1 Step -1
If myRange(i).Interior.ColorIndex = 4 Then
myRange(i).EntireRow.Delete
End If
Next i

     Application.Calculation = xlCalculationAutomatic
End Sub
 
Upvote 0
Could you provide a copy of your sheet using the XL2BB add in or alternatively, share a copy of your workbook via DropBox, Google Drive or similar file sharing platform?
 
Upvote 0
I can't send over the actual data, but here is a make shift one I created. Essentially, I want the macro to delete all of the green rows.

TEST.xlsm
ABCDEFGHIJKLMNO
1Collaborator Full Name
2E
3A
4A
5E
6A
7A
8E
9A
10A
11G
12A
13A
14U
15A
16A
17I
18A
19A
20G
21A
22A
23E
24A
25A
26C
27A
28A
29L
30A
31A
32A
33A
34A
35A
36A
37M
38A
39A
40A
41A
42A
43A
44P
45B
46B
47B
48B
49B
50B
51A
52A
53A
54A
55A
56A
57A
58A
59A
60A
61C
62C
63C
64C
65C
66C
67C
68C
69A
70A
71A
72A
73A
74A
75A
76A
77A
78A
79A
80A
81A
82A
83A
84A
85A
86A
87A
88A
89D
90D
91D
92D
93D
94D
95D
96D
97D
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
H2:H97Cell Valuenot containing "A"textNO
 
Upvote 0
Actually, let me give you the original problem. The are duplicate numbers in column A, and I only need 1 row per number. The row I need to keep is the row that contains "A" in column H.

I use this macro to rid myself of the duplicates

VBA Code:
Sub Delete_duplicate_rows()
Dim Rng As Range
Set Rng = Selection
Rng.RemoveDuplicates Columns:=Array(1), Header:=xlYes
End Sub

But it will only keep the top number and delete the others. Sometimes that works as A is the top row, but other times it doesn't work.

Any suggestions?


TEST.xlsm
ABCDEFGHIJKLMNO
1NumberCollaborator Full Name
21E
31A
41B
51E
65A
75B
85E
98A
109A
1110G
1210A
1310B
1410U
1514A
1615A
1716A
1816B
1916C
Sheet1
 
Upvote 0
If you want to go down the delete by colour path, then try the following (just change the sheet name & RGB colour to match what you've got)
VBA Code:
Option Explicit
Sub mcintosh_Delete_By_Colour()
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")           '<~~ *** Change sheet name to suit ***
    With ws.Range("H1", ws.Cells(Rows.Count, "H").End(xlUp))
        .AutoFilter 1, RGB(0, 255, 0), 8    '<~~ *** Change RGB number to match your colour ***
        If ws.Cells(Rows.Count, "H").End(xlUp).Row > 1 Then
            .Offset(1).EntireRow.Delete
        End If
        .AutoFilter
    End With
End Sub

Before:
mcintosh.xlsm
ABCDEFGH
1Collaborator Full Name
2E
3A
4A
5E
6A
7A
8E
9A
10A
11G
12A
13A
14U
15A
16A
17I
18A
19A
20G
21A
22A
23E
24A
25A
26C
27A
28A
29L
30A
31A
32A
33A
34A
35A
36A
37M
38A
39A
40A
41A
42A
43A
44P
45B
46B
47B
48B
49B
50B
51A
52A
53A
54A
55A
56A
57A
58A
59A
60A
61C
62C
63C
64C
65C
66C
67C
68C
69A
70A
71A
72A
73A
74A
75A
76A
77A
78A
79A
80A
81A
82A
83A
84A
85A
86A
87A
88A
89D
90D
91D
92D
93D
94D
95D
96D
97D
Sheet1


After:
mcintosh.xlsm
ABCDEFGH
1Collaborator Full Name
2A
3A
4A
5A
6A
7A
8A
9A
10A
11A
12A
13A
14A
15A
16A
17A
18A
19A
20A
21A
22A
23A
24A
25A
26A
27A
28A
29A
30A
31A
32A
33A
34A
35A
36A
37A
38A
39A
40A
41A
42A
43A
44A
45A
46A
47A
48A
49A
50A
51A
52A
53A
54A
55A
56A
57A
58A
59A
60A
61A
62A
63
Sheet1


Alternatively, if you wanted to delete using the original data - with no reference to the colour - then try this (just change the sheet name to suit)
VBA Code:
Option Explicit
Sub mcintosh_Delete_By_Data()
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet2")           '<~~ *** Change sheet name to suit ***
    Dim r As Range, i As Long, a, b As Range
    a = ws.Range("A1", ws.Cells(Rows.Count, "H").End(xlUp))
    Set b = ws.Range("A2", ws.Cells(Rows.Count, "A").End(xlUp))
    
    For i = 2 To UBound(a, 1)
        If WorksheetFunction.CountIf(b, CStr(a(i, 1))) > 1 And a(i, 8) <> "A" Then
            If r Is Nothing Then
                Set r = ws.Cells(i, 1)
            Else
                Set r = Union(r, ws.Cells(i, 1))
            End If
        End If
    Next i
    If Not r Is Nothing Then
        r.EntireRow.Delete
    End If
End Sub

Before:
mcintosh.xlsm
ABCDEFGH
1NumberCollaborator Full Name
21E
31A
41B
51E
65A
75B
85E
98A
109A
1110G
1210A
1310B
1410U
1514A
1615A
1716A
1816B
1916C
20
Sheet2


After:
mcintosh.xlsm
ABCDEFGH
1NumberCollaborator Full Name
21A
35A
48A
59A
610A
714A
815A
916A
10
Sheet2
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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