Solved

Delete not working

Posted on 2013-06-07
3
205 Views
Last Modified: 2013-06-10
I am trying to use the code below to delete rows from teh Accounts sheet where matching cusip in Compare sheet Column AP is null.  




Sub DeleteMissingAccounts()

  Dim lngRow                                            As Long
  Dim objCell                                           As Range
  Dim objRange                                          As Range
 
 
   Set objRange = Range(Sheets("Compare").[c3], Sheets("Compare").Cells(Sheets("Compare").Cells.Rows.Count, "C").End(xlUp))
 
 If Not (objRange Is Nothing) Then
     Sheets("Accounts").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(Sheets("Compare").Cells(objCell.Row, "AP"))) Then
               Sheets("Accounts").Rows(lngRow).EntireRow.Delete
            End If
         End If        
     Next lngRow
     
 End If '
0
Comment
Question by:leezac
[X]
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
  • 2
3 Comments
 
LVL 22

Accepted Solution

by:
rspahitz earned 500 total points
ID: 39230806
I suspect that the problem is that when you delete a row, the next one shifts up, but the for-next goes on to the next row, skipping it.

You may want to decrement the counter when you delete a row:

            If Not (IsEmpty(Sheets("Compare").Cells(objCell.Row, "AP"))) Then
               Sheets("Accounts").Rows(lngRow).EntireRow.Delete
               lngRow = lngRow - 1
            End If 

Open in new window

0
 

Author Closing Comment

by:leezac
ID: 39233587
That fixed it .
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 39234852
awesome...that was one of those trick questions in college and it finally paid off :)
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

724 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