+ Reply to Thread
Results 1 to 3 of 3

delete from : to n rows after finding data in a cell

  1. #1
    George
    Guest

    delete from : to n rows after finding data in a cell

    I want to find some text in col A and delete all rows until i find test in
    col D.
    e.g
    find "Bank account name" in col A.
    Then delete all rows down to "closing balance" in col d (do not delete Col d)
    Any suggestions? cheers

  2. #2
    Dave Peterson
    Guest

    Re: delete from : to n rows after finding data in a cell

    Do not delete column D--does that mean don't delete the row with closing balance
    on it?

    This seems to work ok for me:

    Option Explicit
    Sub testme()
    Dim FoundCellTop As Range
    Dim FoundCellBot As Range
    Dim WhatToFindTop As String
    Dim WhatToFindBot As String

    WhatToFindTop = "Bank Account Name"
    WhatToFindBot = "Closing Balance"

    With Worksheets("sheet1")
    With .Range("a:a")
    Set FoundCellTop = .Cells.Find(what:=WhatToFindTop, _
    after:=.Cells(.Cells.Count), _
    LookIn:=xlValues, _
    lookat:=xlWhole, _
    searchorder:=xlByRows, _
    searchdirection:=xlNext, _
    MatchCase:=False)
    End With

    With .Range("d:d")
    Set FoundCellBot = .Cells.Find(what:=WhatToFindBot, _
    after:=.Cells(.Cells.Count), _
    LookIn:=xlValues, _
    lookat:=xlWhole, _
    searchorder:=xlByRows, _
    searchdirection:=xlNext, _
    MatchCase:=False)
    End With


    If FoundCellTop Is Nothing _
    Or FoundCellBot Is Nothing Then
    MsgBox "At least one wasn't found!"
    Exit Sub
    End If

    If (FoundCellBot.Row - FoundCellTop.Row) < 2 Then
    MsgBox "nothing to delete"
    Else
    .Range(FoundCellTop.Offset(1, 0), _
    FoundCellBot.Offset(-1, 0)).EntireRow.Delete
    End If
    End With

    End Sub



    George wrote:
    >
    > I want to find some text in col A and delete all rows until i find test in
    > col D.
    > e.g
    > find "Bank account name" in col A.
    > Then delete all rows down to "closing balance" in col d (do not delete Col d)
    > Any suggestions? cheers


    --

    Dave Peterson

  3. #3
    George
    Guest

    Re: delete from : to n rows after finding data in a cell

    Genius!
    Thanks Dave

    "Dave Peterson" wrote:

    > Do not delete column D--does that mean don't delete the row with closing balance
    > on it?
    >
    > This seems to work ok for me:
    >
    > Option Explicit
    > Sub testme()
    > Dim FoundCellTop As Range
    > Dim FoundCellBot As Range
    > Dim WhatToFindTop As String
    > Dim WhatToFindBot As String
    >
    > WhatToFindTop = "Bank Account Name"
    > WhatToFindBot = "Closing Balance"
    >
    > With Worksheets("sheet1")
    > With .Range("a:a")
    > Set FoundCellTop = .Cells.Find(what:=WhatToFindTop, _
    > after:=.Cells(.Cells.Count), _
    > LookIn:=xlValues, _
    > lookat:=xlWhole, _
    > searchorder:=xlByRows, _
    > searchdirection:=xlNext, _
    > MatchCase:=False)
    > End With
    >
    > With .Range("d:d")
    > Set FoundCellBot = .Cells.Find(what:=WhatToFindBot, _
    > after:=.Cells(.Cells.Count), _
    > LookIn:=xlValues, _
    > lookat:=xlWhole, _
    > searchorder:=xlByRows, _
    > searchdirection:=xlNext, _
    > MatchCase:=False)
    > End With
    >
    >
    > If FoundCellTop Is Nothing _
    > Or FoundCellBot Is Nothing Then
    > MsgBox "At least one wasn't found!"
    > Exit Sub
    > End If
    >
    > If (FoundCellBot.Row - FoundCellTop.Row) < 2 Then
    > MsgBox "nothing to delete"
    > Else
    > .Range(FoundCellTop.Offset(1, 0), _
    > FoundCellBot.Offset(-1, 0)).EntireRow.Delete
    > End If
    > End With
    >
    > End Sub
    >
    >
    >
    > George wrote:
    > >
    > > I want to find some text in col A and delete all rows until i find test in
    > > col D.
    > > e.g
    > > find "Bank account name" in col A.
    > > Then delete all rows down to "closing balance" in col d (do not delete Col d)
    > > Any suggestions? cheers

    >
    > --
    >
    > Dave Peterson
    >


+ 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