Macro to delete entirerow from Sheet2 if Sheet1 contains value ?

Coyotex3

Active Member
Joined
Dec 12, 2021
Messages
496
Office Version
  1. 365
Platform
  1. Windows
Hello, I'm trying to create a macro that will look At sheet1 "Column A" and go down each cell with data and if on Sheet1 Range("A6").value = "James" then look at sheet2 and delete the entire row if it finds "James" on sheet2.
If Range("A10").value = "Jane" then delete the entire row if it finds "Jane" on sheet2 etc.

Like This

Book1
ABCDE
1Report
28-Feb
3User
4
5NameOrderAmount
6James10100
7James15150
8James15150
9James15150
10
11
12
13John20200
14John20200
15John20200
16John20200
17John20200
18John20200
19John20200
20John20200
21
22
23
24Jane20200
25Jane20200
26Jane20200
27Jane20200
28Jane20200
29Jane20200
30Jane20200
31Jane20200
32
33
34
35
36
37
38
Sheet1


To This

Book1
ABCD
1Report
28-Feb
3User
4
5NameOrderAmount
6John20200
7John20200
8John20200
9John20200
10John20200
11John20200
12John20200
13John20200
14
15
Sheet1


Sheet2 will look like this

Book1
ABC
1Names
2James
3James
4James
5Jane
6Jane
7Jane
Sheet2


Any ideas?
 
Last edited:

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Sorry, I meant to say that I need a macro to look At sheet2 "Column A" and go down each cell with data and if on Sheet2 Range("A6").value = "James" then look at sheet1 and delete the entire row if it finds "James" on sheet1.
 
Upvote 0
Here is what I have came up so far.
VBA Code:
Sub Delete()
 Dim donot(2) As Variant
 Dim sh As Worksheet
 Dim sht As Worksheet
 
 Set sh = Sheets("sheet2")
  Set sht = Sheets("sheet1")
 
 donot(1) = sh.Range("A2").Value
 donot(2) = sh.Range("A4").Value
With ActiveSheet
    For i = 1 To 2
        .AutoFilterMode = False
        With Range("A6", Range("A" & Rows.Count).End(xlUp))
            .AutoFilter 1, "*" & donot(i) & "*"
            On Error Resume Next
            .Offset(1).SpecialCells(12).EntireRow.Delete
        End With
        .AutoFilterMode = False
    Next i
End With
End Sub

Can anybody help me make it dynamic? Sometimes sheet2 will have more than 2 names. It may have 2 this week but 15 next week.
 
Upvote 0
Found this code on StackOverflow and with a little tailoring I was able to get it to do what I needed.

VBA Code:
Sub delete_selected_rows()
    Dim rng1 As Range, rng2 As Range, rngToDel As Range, c As Range
    Dim lastRow As Long

    With Worksheets("sheet1")
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        Set rng1 = .Range("A6:A" & lastRow)
    End With

    Set rng2 = Worksheets("sheet2").Range("A:A")

    For Each c In rng1
        If Not IsError(Application.Match(c.Value, rng2, 0)) Then
            'if value from rng1 is found in rng2 then remember this cell for deleting
            If rngToDel Is Nothing Then
                Set rngToDel = c
            Else
                Set rngToDel = Union(rngToDel, c)
            End If
        End If
    Next c

    If Not rngToDel Is Nothing Then rngToDel.EntireRow.Delete
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,407
Messages
6,124,723
Members
449,184
Latest member
COrmerod

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