Solved

Delete from Sheet1 based on value in Sheet2

Posted on 2013-05-20
5
488 Views
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.
Not-Null.zip
0
Comment
Question by:leezac
  • 3
  • 2
5 Comments
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 39181434
Hi,

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

flour
soda


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

Thanks.

BFN,

fp.

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

Author Comment

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

flour
soda

Thanks
0
 
LVL 35

Accepted Solution

by:
[ 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
     Worksheets("Sheet1").Select
  
     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
               Worksheets("Sheet1").Rows(lngRow).EntireRow.Delete
            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.

BFN,

fp.
Q-28133386.xls
0
 

Author Closing Comment

by:leezac
ID: 39182266
Thank you - works perfect
0
 
LVL 35

Expert Comment

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

Good luck with the rest of your project.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
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…

828 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