+ Reply to Thread
Results 1 to 3 of 3

Unable to delete rows

Hybrid View

  1. #1
    Registered User
    Join Date
    05-22-2012
    Location
    Lincoln, NE
    MS-Off Ver
    Excel 2010
    Posts
    11

    Unable to delete rows

    The code below is meant to find an item that exists in one sheet, but not another. If this is the case, I want to cut and paste that row to another sheet, and then delete the empty row that is left behind. This is what I have currently:

    Dim contract1 as Range
    Dim match2 as Range
    Dim j as Long
    
    j = 2
    Set contract1 = ActiveWorkbook.Sheets("UpdatedWS").Cells(j, 3)
    'Cut and paste new policies into Differences worksheet and _
     delete rows left empty
    For Each contract1 In Sheets("UpdatedWS").Range("C2", "C43")
        With Sheets("Current").Range("C:C")
            Set match2 = .Find(What:=contract1, _
                        After:=.Cells(.Cells.Count), _
                        LookIn:=xlValues, _
                        LookAt:=xlWhole, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlNext, _
                        MatchCase:=False)
            If Not match2 Is Nothing Then
            Else
                DiffCounter = DiffCounter + 1
                contract1.EntireRow.Cut _
                    Sheets("Differences").Cells(DiffCounter + 2, 1)
                Rows(contract1).Select
                Selection.Delete Shift:=xlUp
            End If
        End With
    Next contract1
    When the code runs, however, I am left with a blank row in "UpdatedWS" that never got deleted.
    Where am I going wrong?

  2. #2
    Forum Contributor
    Join Date
    08-04-2011
    Location
    Tampa, FL
    MS-Off Ver
    Excel 2007
    Posts
    109

    Re: Unable to delete rows

    The problem could be that you are searching top-down. When you delete a row, it then messes up your loop iteration. If you work from the bottom row and work your way up, it won't interfere with your count cycle when deleting. Probably not a clear explaination, but when looping through rows and deleting, it is always best to work bottom-up.

  3. #3
    Registered User
    Join Date
    05-22-2012
    Location
    Lincoln, NE
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Unable to delete rows

    Right, since I am shifting up, each row below the deleted row moves up one number. Instead of deleting rows, I'm just re-filtering the sheet after rows are deleted, effectively shifting all empty rows to the bottom. It's working perfectly, so I think I can say this issue is resolved.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1