+ Reply to Thread
Results 1 to 26 of 26

Find keyword and delete the rows below and above

Hybrid View

zeroist Find keyword and delete the... 09-09-2013, 08:34 AM
patel45 Re: Find keyword and delete... 09-09-2013, 09:43 AM
zeroist Re: Find keyword and delete... 09-09-2013, 05:41 PM
patel45 Re: Find keyword and delete... 09-10-2013, 02:46 AM
HaHoBe Re: Find keyword and delete... 09-10-2013, 02:52 AM
HaHoBe Re: Find keyword and delete... 09-10-2013, 03:40 AM
zeroist Re: Find keyword and delete... 09-10-2013, 08:18 AM
zeroist Re: Find keyword and delete... 09-10-2013, 05:21 PM
HaHoBe Re: Find keyword and delete... 09-11-2013, 07:09 AM
zeroist Re: Find keyword and delete... 09-11-2013, 06:43 AM
zeroist Re: Find keyword and delete... 09-11-2013, 08:20 AM
HaHoBe Re: Find keyword and delete... 09-11-2013, 09:01 AM
zeroist Re: Find keyword and delete... 09-11-2013, 09:38 AM
HaHoBe Re: Find keyword and delete... 09-11-2013, 09:46 AM
zeroist Re: Find keyword and delete... 09-12-2013, 02:27 AM
HaHoBe Re: Find keyword and delete... 09-12-2013, 02:44 AM
zeroist Re: Find keyword and delete... 09-12-2013, 10:18 AM
HaHoBe Re: Find keyword and delete... 09-12-2013, 10:29 AM
zeroist Re: Find keyword and delete... 09-12-2013, 12:55 PM
HaHoBe Re: Find keyword and delete... 09-12-2013, 01:58 PM
zeroist Re: Find keyword and delete... 09-12-2013, 02:54 PM
zeroist Re: Find keyword and delete... 09-12-2013, 03:16 PM
HaHoBe Re: Find keyword and delete... 09-12-2013, 03:54 PM
zeroist Re: Find keyword and delete... 09-13-2013, 03:04 AM
HaHoBe Re: Find keyword and delete... 09-13-2013, 03:35 AM
zeroist Re: Find keyword and delete... 09-13-2013, 03:45 AM
  1. #1
    Registered User
    Join Date
    09-05-2012
    Location
    Türkiye
    MS-Off Ver
    Excel 2013
    Posts
    80

    Re: Find keyword and delete the rows below and above

    I am sorry for my english.I am trying to do my best for my english.
    Row 12 still exist because I am only interested in first keyword (keyword in row is:"there is no guarentee") which is in row 5.

    http://sketchtoy.com/49962974

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Find keyword and delete the rows below and above

    Hi, zeroist,

    maybe this code will perform as you wish:

    Sub SearchItemsInWorkbook(strSearch As String, lngOffNeg As Long, lngOffPos As Long, blnDelete As Boolean, lngFirst As Long)
    ' strSearch: item to search for as part of teh cells
    ' lngOffNeg: number of rows above the found cell to be deleted, should be entered as positiv number!
    ' lngOffPos: number of rows below the found cell to be deleted, should be entered as positiv number!
    ' blnDelete: deicdes on TRUE to delete or FALSE to color
    ' lngFirst:  0 deletes any occurrance of the item
    '            1 deletes the first occurrance of the item
    '            2 deletes the last occurrance of the item
    
    Dim wsTab As Worksheet
    Dim rngCell As Range
    Dim rngDelete As Range
    Dim strAddress As String
    Dim strTempAdr As String
    Dim blnLoopOn As Boolean
    Dim blnFirst As Boolean
    
    If lngFirst < 0 And lngFirst > 2 Then
      MsgBox "Value for parameter of what to delete is out of range available (must be 0 for all, 1 for first, 2 for last)!"
      Exit Sub
    End If
    
    For Each wsTab In ActiveWorkbook.Worksheets
      blnLoopOn = True
      Set rngCell = wsTab.Cells.Find(what:=strSearch, lookAt:=xlPart)
      If Not rngCell Is Nothing Then
        Set rngDelete = rngCell
        Set rngDelete = Union(rngDelete, rngCell.Offset(-lngOffNeg, 0).Resize(lngOffNeg + 1))
        Set rngDelete = Union(rngDelete, rngCell.Resize(lngOffPos + 1))
        Select Case lngFirst
          Case 0, 2
            strAddress = rngCell.Address
            Do While blnLoopOn
              Set rngCell = wsTab.Cells.FindNext(After:=rngCell)
              blnLoopOn = rngCell.Address <> strAddress
              If Not rngCell Is Nothing And rngCell.Address <> strAddress Then
                strTempAdr = rngCell.Address
                Set rngDelete = Union(rngDelete, rngCell.Offset(-lngOffNeg, 0).Resize(lngOffNeg + 1))
                Set rngDelete = Union(rngDelete, rngCell.Resize(lngOffPos + 1))
              Else
                blnLoopOn = False
              End If
            Loop
          Case Else
        End Select
      End If
      If lngFirst = 2 Then
        If Len(strTempAdr) > 0 Then
          Set rngDelete = wsTab.Range(strTempAdr)
          Set rngDelete = Union(rngDelete, rngCell.Offset(-lngOffNeg, 0).Resize(lngOffNeg + 1))
          Set rngDelete = Union(rngDelete, rngCell.Resize(lngOffPos + 1))
        End If
      End If
      If Not rngDelete Is Nothing Then
        If blnDelete Then
          rngDelete.EntireRow.Delete
        Else
          rngDelete.Interior.ColorIndex = 6
        End If
      End If
      Set rngDelete = Nothing
    Next wsTab
    
    Set rngCell = Nothing
    
    End Sub
    Sub EF953570_3()
    'will call procedure and hand over item to serach, the number of rows above (eneterd asp positive number) as
    'well as number of rows below found to delete and advise to delete or just color
    Call SearchItemsInWorkbook("there is no guarentee", 3, 0, True, 1)
    Call SearchItemsInWorkbook("ringtone to your cell", 0, 3, True, 2)
    End Sub
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

+ 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. Replies: 6
    Last Post: 03-18-2014, 11:16 AM
  2. Macro To Find & Copy Rows Based On A Keyword List
    By Lilfish in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-02-2013, 10:02 AM
  3. Replies: 4
    Last Post: 09-18-2012, 10:54 AM
  4. Replies: 2
    Last Post: 07-13-2012, 04:02 AM
  5. VBA Popup to search a sheet and delete rows based on keyword entered
    By MichaelJG in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-05-2011, 08:08 AM

Tags for this Thread

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