if sheet2 data is present in sheet1 then delete that complete row

if sheet2 data is present in sheet1 then delete that complete row
this highlighted colour is only for understanding
i have to do this by vba
Book3.xlsm
Avinash SinghAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
Here is a small VBA procedure that will do what you wanted for that data set.

Sub DeleteMatchingRows()
    ' Define local variable
    Dim shtMain As Worksheet
    Dim shtData As Worksheet
    Dim rngMain As Range
    Dim rngData As Range
    Dim lngRow As Long

    ' Turn off screen updating
    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False

    ' Work with Sheet1 and Sheet2
    Set shtMain = ThisWorkbook.Worksheets(1)
    Set shtData = ThisWorkbook.Worksheets(2)
    
    ' Key values found in first column, starting at first row
    Set rngMain = shtMain.Range(shtMain.Cells(1, 1), shtMain.Cells(shtMain.Cells(shtMain.Rows.Count, 1).End(xlUp).Row, 1))
    Set rngData = shtData.Range(shtData.Cells(1, 1), shtData.Cells(shtData.Cells(shtData.Rows.Count, 1).End(xlUp).Row, 1))

    ' Check value from first sheet against second, delete row if match found
    For lngRow = rngMain.Rows.Count To 1 Step -1
        If Not IsError(Application.VLookup(rngMain.Cells(lngRow, 1), rngData, 1, False)) Then
            rngMain.Rows(lngRow).EntireRow.Delete
        End If
    Next

    ' Turn on screen updating
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
End Sub

Open in new window


»bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Avinash SinghAuthor Commented:
Thnx Bill Prew Sir for giving ur precious time to this post
0
Bill PrewIT / Software Engineering ConsultantCommented:
Welcome.


»bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.