Delete from Sheet1 based on value in Sheet2

Posted on 2013-05-20
Last Modified: 2013-05-20
I have the attached example of a file where I need to

do the below:

Delete rows to end of used rows on Sheet1 where
Sheet1 column A matches Sheet2 column C
And Sheet2 column AP is not null

Can someone please help on how best to approach this either using a formula or VBA - preferably vba.  

Thanks in advance.
Question by:leezac
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 35

Expert Comment

by:[ fanpages ]
ID: 39181434

Just to confirm your expected outcome; in the example workbook provided, there would just be two remaining rows in [Sheet1]:


...or would "Milk" still be present (as it does not match "milk" in [Sheet2])?




PS. Should "beens" read "beans"? :)

Author Comment

ID: 39181588
expected outcome;  there would just be two remaining rows in [Sheet1]:


LVL 35

Accepted Solution

[ fanpages ] earned 500 total points
ID: 39181712
Hi again,

Thanks for your confirmation.

This code should meet your requirements:

Option Explicit
Public Sub Q_28133386()

  Dim lngRow                                            As Long
  Dim objCell                                           As Range
  Dim objRange                                          As Range
  On Error Resume Next
  Application.ScreenUpdating = False
  Set objRange = Range(Worksheets("Sheet2").[C3], Worksheets("Sheet2").Cells(Worksheets("Sheet2").Cells.Rows.Count, "C").End(xlUp))
  If Not (objRange Is Nothing) Then
     For lngRow = Cells(Cells.Rows.Count, 1).End(xlUp).Row To 2& Step -1&
         Set objCell = Nothing
         Set objCell = objRange.Find(What:=Cells(lngRow, "A"))
         If Not (objCell Is Nothing) Then
            If Not (IsEmpty(Worksheets("Sheet2").Cells(objCell.Row, "AP"))) Then
            End If ' If IsEmpty(Worksheets("Sheet2").Cells(objCell.Row, "AP")) Then
         End If ' If Not (objCell Is Nothing) Then
     Next lngRow
  End If ' If Not (objRange Is Nothing) Then
  Set objCell = Nothing
  Set objRange = Nothing
  Application.ScreenUpdating = True
End Sub

Open in new window

I have also attached a workbook based on your original with this code included within a separate code module, "basQ_28133386", for convenience.



Author Closing Comment

ID: 39182266
Thank you - works perfect
LVL 35

Expert Comment

by:[ fanpages ]
ID: 39182404
You are very welcome.

Good luck with the rest of your project.

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

738 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