+ Reply to Thread
Results 1 to 13 of 13

Delete Every Nth Row

Hybrid View

  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Delete Every Nth Row

    Hello everybody
    I found this code
    Public Sub DeleteEveryNthRow(Optional N As Long = 2)
            'The default is to delete every 2nd row
            Dim rDelete As Range
            Dim i As Long
            With ActiveSheet
                Set rDelete = .Rows(N)
                For i = 2 * N To .UsedRange.Rows.Count Step N
                    Set delRange = Union(rDelete, .Rows(i))
                Next i
            End With
            rDelete.Delete
        End Sub
    It's supposed to delete every Nth row ..
    I don't know how to use it?
    I tried a lot but failed

  2. #2
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Delete Every Nth Row

    You need to call it from another sub, given that you have parameters involved. Try:
    Sub test()
        Call DeleteEveryNthRow      'or, DeleteEveryNthRow(3) for every third row
    End Sub
    
    Public Sub DeleteEveryNthRow(Optional N As Long = 2)
        'The default is to delete every 2nd row
        Dim rDelete As Range
        Dim i As Long
        With ActiveSheet
            Set rDelete = .Rows(N)
            For i = 2 * N To .UsedRange.Rows.Count Step N
                Set rDelete = Union(rDelete, .Rows(i))  'made minor edit
            Next i
        End With
        rDelete.Delete
    End Sub

  3. #3
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Delete Every Nth Row

    Mr. Berlan
    That's great and simple .. So perfect
    Thanks a lot
    I tried the code with 10000 rows .. It took very long time and I had to press Ctrl + Pause Break to stop it .. And no rows had been deleted!
    Is there a faster code that can do that task?

  4. #4
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Delete Every Nth Row

    Hi,

    I rewrote it a bit and added turned of screenupdating and calculation, etc. during the procedure (which you can integrate in your first script instead if needed). Added a parameter t

    Try this:
    Sub test()
    
        Call DeleteEveryNthRow                          'every 2nd row on activesheet
        'Call DeleteEveryNthRow(, 3)                         'every 3rd row on activesheet
        'Call DeleteEveryNthRow(Sheets("Sheet1"), 3)    'every 3rd row on Sheet1
        'Call DeleteEveryNthRow(Sheets("Sheet3"), 5)    'every 5th row on Sheet3
    
    End Sub
    
    
    Public Sub DeleteEveryNthRow(Optional ws As Worksheet = Nothing, Optional N As Long = 2)
    
        Dim lCalc As Long, rngDel As Range, rngRow As Range
        If ws Is Nothing Then Set ws = ActiveSheet
        
        'optimize
        With Application
            .EnableEvents = False
            .DisplayAlerts = False
            lCalc = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
        End With
    
        For Each rngRow In ws.UsedRange.Rows
            If (rngRow.Row Mod N) = 0 Then
                If rngDel Is Nothing Then
                    Set rngDel = rngRow
                Else
                    Set rngDel = Union(rngRow, rngDel)
                End If
            End If
        Next rngRow
    
        If Not rngDel Is Nothing Then rngDel.EntireRow.Delete
    
        'restore settings
        With Application
            .EnableEvents = True
            .DisplayAlerts = True
            .Calculation = lCalc
            .ScreenUpdating = True
        End With
    
    End Sub
    Best,
    berlan
    Last edited by berlan; 03-04-2015 at 11:23 AM. Reason: Notice minor edit

  5. #5
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Delete Every Nth Row

    Does this help?

    Sub YasserKhalil()
    Dim i As Long
    For i = Range("A" & Rows.Count).End(3).Row To 2 Step -9
            Rows(i).Delete
    Next i
    End Sub

  6. #6
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Delete Every Nth Row

    @John, only problem with that one is that it will recalculate every time you delete a row and slow down the process even more. Or if calculation is turned off, there will still be more interaction with Excel for each deletion, I'm afraid, but haven't tried on a large sample.

    berlan

  7. #7
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Delete Every Nth Row

    I tested on 10000 rows. It took less then 15 seconds. Of course room for improvement. But my first goal was to get the rows deleted.

  8. #8
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Delete Every Nth Row

    Thanks everybody for these great help
    I tried a combination of both codes and it took only 7 seconds for me
    Sub YasserKhalil()
        Dim I As Long, lCalc
    
        With Application
            .EnableEvents = False
            .DisplayAlerts = False
            lCalc = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
        End With
    
        For I = Range("A" & Rows.Count).End(3).Row To 2 Step -2
                Rows(I).Delete
        Next I
        
        With Application
            .EnableEvents = True
            .DisplayAlerts = True
            .Calculation = lCalc
            .ScreenUpdating = True
        End With
    End Sub

  9. #9
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Delete Every Nth Row

    You're welcome. I thought you wanted every 9th row, the code you posted is for every 2 rows.

  10. #10
    Forum Expert Debraj Roy's Avatar
    Join Date
    09-27-2012
    Location
    New Delhi,India
    MS-Off Ver
    Excel 2013
    Posts
    1,469

    Re: Delete Every Nth Row

    Here is another way.. for testing..

    Sub EF1069951()
    
    Dim rngTargetRange As Range
    Dim lngNth As Long
    Dim rngHelperCell As Range
    Set rngTargetRange = Range("A1").CurrentRegion
    lngNth = 9
    
        With rngTargetRange
            Set rngHelperCell = .Cells(1).Offset(1, .Columns.Count + 2)
            rngHelperCell = "=MOD(row(" & .Cells(2, 1).Address(0, 1) & ")," & lngNth & ")=0"
            .AdvancedFilter xlFilterInPlace, .Cells(1).Offset(, .Columns.Count + 2).Resize(2)
            .Offset(1).SpecialCells(12).Delete (xlShiftUp)
            rngHelperCell.Clear
        End With
    
    If ActiveSheet.FilterMode = True Then _
        ActiveSheet.ShowAllData
    
    End Sub
    Regards!
    =DEC2HEX(3563)

    If you like someone's answer, click the star to give them a reputation point for that answer...

  11. #11
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Delete Every Nth Row

    Thanks a lot Mr. Debraj for this code
    I tried to make it delete every 2nd row by changing this line
    lngNth = 2
    But I encountered an error (Object required) at this line
    rngHelperCell.Clear

  12. #12
    Forum Expert Debraj Roy's Avatar
    Join Date
    09-27-2012
    Location
    New Delhi,India
    MS-Off Ver
    Excel 2013
    Posts
    1,469

    Re: Delete Every Nth Row

    ok.. I missed that part..

    Option Explicit
    
    Sub EF1069951()
    
    Dim rngTargetRange As Range
    Dim lngNth As Long
    Dim rngHelperCell As Range
    Set rngTargetRange = Range("A1").CurrentRegion
    lngNth = 2
    
        With rngTargetRange
            Set rngHelperCell = .Cells(1).Offset(1, .Columns.Count + 2)
            rngHelperCell = "=MOD(row(" & .Cells(2, 1).Address(0, 1) & ")," & lngNth & ")=0"
            .AdvancedFilter xlFilterInPlace, .Cells(1).Offset(, .Columns.Count + 2).Resize(2)
            .Offset(1).SpecialCells(12).Delete (xlShiftUp)
            On Error Resume Next: rngHelperCell.Clear: On Error GoTo 0
        End With
    
    If ActiveSheet.FilterMode = True Then _
        ActiveSheet.ShowAllData
    
    End Sub

  13. #13
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Delete Every Nth Row

    Thanks a lot Mr. Debraj Roy

    The code in post #8# takes about 3 seconds but your working code takes about 6 seconds !
    Although I added the parts of disabling screenupdating and calculation for your code .. But I preferred your way of thinking about using filter
        With Application
            .EnableEvents = False
            .DisplayAlerts = False
            lCalc = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
        End With
        With Application
            .EnableEvents = True
            .DisplayAlerts = True
            .Calculation = lCalc
            .ScreenUpdating = True
        End With

+ 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. VBA code to delete the FILTERED range and moving the cell up (dont Delete entire row)
    By mchilapur in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-14-2015, 07:48 AM
  2. Replies: 1
    Last Post: 11-30-2013, 06:37 AM
  3. Macro to delete sheets and saves remaining file does not properly delete module
    By pherrero in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-21-2005, 08:12 PM
  4. Replies: 0
    Last Post: 06-21-2005, 01:05 PM
  5. Re: Macro to delete sheets and saves remaining file does not properly delete module
    By pherrero in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-21-2005, 01:05 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