+ Reply to Thread
Results 1 to 3 of 3

Delete Rows in Sequence

Hybrid View

  1. #1
    Valued Forum Contributor MaczaQ's Avatar
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    510

    Re: Delete Rows in Sequence

    Hi Jakila23

    Let your data will be in sheet1 in column A

    Try this code for delete rows you need. Copy it to Module and start delete_rows()

    Sub delete_rows()
    Dim allow
    Dim forDelete As New Collection 'a list of rows for delete
    allow = Array("JAN", "APR", "JUL", "OCT") 'this months will stay
    
    For r = 1 To Sheets("Sheet1").UsedRange.Rows.Count
        If in_array(allow, Left(Sheets("sheet1").Cells(r, 1).Value, 3)) = -1 Then 'if month is not in array then will be take as row for delete
            forDelete.Add r
        End If
    Next r
    
    For i = forDelete.Count To 1 Step -1
        Sheets("Sheet1").Rows(forDelete(i) & ":" & forDelete(i)).delete shift:=xlUp 'delete rows from last to first
    Next i
    
    End Sub
    
    Function in_array(ByRef allow, val As String) As Integer
    in_array = -1
        For i = 0 To UBound(allow)
            If UCase(val) = allow(i) Then
                in_array = i
                Exit For
            End If
        Next i
    End Function
    Best Regards

  2. #2
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Delete Rows in Sequence

    Assuming these data in 1 column:

    Sub tst()
      sn = Application.Transpose(Columns(1).SpecialCells(2))
    
      For j = 1 To 12 Step 3
        sn = Filter(Filter(sn, LCase(Application.GetCustomListContents(3)(j + 1)), False), LCase(Application.GetCustomListContents(3)(j + 2)), False)
      Next
    
      Cells(1, 5).Resize(UBound(sn) + 1) = Application.Transpose(sn)
    End Sub



+ 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