+ Reply to Thread
Results 1 to 6 of 6

Code to delete rows only works sometimes

Hybrid View

  1. #1
    Registered User
    Join Date
    05-24-2013
    Location
    Antwerp, Belgium
    MS-Off Ver
    Excel 2007
    Posts
    6

    Code to delete rows only works sometimes

    Dear all,

    I wrote the following code to copy a range of cells from one tab to another one (below a list) and delete a few rows based on the cell value of another column (and update a few pivots, hide a few rows in other tabs). The deleted rows are duplicates in the list on tab "RTC - Status en opvolging". I identify these duplicaties by using vlookup in the same range (which checks whether there is already the same ordernummer above the cells that have been pasted). If the value of this if(iserror(vlookup)-statement = 1, then the row should be deleted.

    This code worked fine in the beginning, but now it seems that it does not delete the rows anymore even though the value in column K=1. Can anybody help me and point me out what's wrong with the code?

    Thanks in advance,

    Bart

        Sheets("Actual Arrivals").Select
        Range("D7:I7").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Copy
        Sheets("RTC - Status en opvolging").Select
        Range("E6").Select
        Selection.End(xlDown).Offset(1, 0).Select
        ActiveSheet.Paste
        Dim rng As Range, cell As Range, del As Range
        Set rng = Intersect(Range("K:K"), ActiveSheet.UsedRange)
        For Each cell In rng
        If (cell.Value) = "1" _
        Then
        If del Is Nothing Then
        Set del = cell
        Else: Set del = Union(del, cell)
        End If
        End If
        Next cell
        On Error Resume Next
        del.EntireRow.Delete    Sheets("Pivots - Geplande bloktreinen").Select
        ActiveSheet.PivotTables("PivotsBloktreinen").PivotCache.Refresh
        Sheets("Pivots - Template wagonplanning").Select
        ActiveSheet.PivotTables("PivotTemplate1").PivotCache.Refresh
        ActiveSheet.PivotTables("PivotTemplate2").PivotCache.Refresh
        ActiveSheet.PivotTables("PivotTemplate3").PivotCache.Refresh
        ActiveSheet.PivotTables("PivotTemplate4").PivotCache.Refresh
        ActiveSheet.PivotTables("PivotTemplate5").PivotCache.Refresh
        ActiveSheet.PivotTables("PivotTemplate6").PivotCache.Refresh
        ActiveSheet.PivotTables("PivotTemplate7").PivotCache.Refresh
         
        Sheets("Template wagonplanning").Select
        Columns("A:C").Select
        Selection.EntireColumn.Hidden = False
        ActiveSheet.Cells.EntireRow.Hidden = False
        For Each cell In Range("Template1")
        If UCase(cell.Value) = "1" Then
        cell.EntireRow.Hidden = True
        Columns("B:B").EntireColumn.Hidden = True
        ActiveSheet.Cells.Range("A1").Select
        End If
        Next
        End Sub

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Code to delete rows only works sometimes

    All I can see is that you need to indicate a new line here:

    del.EntireRow.Delete    Sheets("Pivots - Geplande bloktreinen").Select
    del.EntireRow.Delete :  Sheets("Pivots - Geplande bloktreinen").Select
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    05-24-2013
    Location
    Antwerp, Belgium
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Code to delete rows only works sometimes

    In the original code the lines are placed under each other, so I don't think this correction is necessary or am I wrong? I really can't figure out why it's not working anymore..If someone is willing to have a look at the original file, I will be glad to send it over.

    Thanks in advance!

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Code to delete rows only works sometimes

    Hi BartSeli,

    I tested this code:

    Sub BartseliDel()
        
        Dim rng As Range, cell As Range, del As Range
        Set rng = Intersect(Range("K:K"), ActiveSheet.UsedRange)
        For Each cell In rng
        If (cell.Value) = "1" _
        Then
        If del Is Nothing Then
        Set del = cell
        Else: Set del = Union(del, cell)
        End If
        End If
        Next cell
        On Error Resume Next
        del.EntireRow.Delete
        
        End Sub
    and it ran like a charm!

  5. #5
    Registered User
    Join Date
    05-24-2013
    Location
    Antwerp, Belgium
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Code to delete rows only works sometimes

    I can't figure it out either. Can it has something to do that the cell value of "1" is calculated in a table of excel 2010?

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Code to delete rows only works sometimes

    Have you single stepped through the code? I love the code by the way - did I rep you for that? - I intended to

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Changing code from deleting rows to cut/paste rows into another sheet and delete blank row
    By kmarshall6576 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-18-2013, 01:54 AM
  2. my code delete all the rows
    By suny100 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-17-2011, 02:35 PM
  3. Code to delete rows based on status code in Column I
    By LemonTwist in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-26-2009, 07:01 PM
  4. Code to delete all rows that have 0
    By BigBas in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-13-2007, 02:25 PM
  5. Code to delete rows
    By shanemjo in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-12-2005, 02:36 PM

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