+ Reply to Thread
Results 1 to 9 of 9

Delete rows with specific text

  1. #1
    Registered User
    Join Date
    08-24-2004
    Posts
    14

    Delete rows with specific text

    Hi,

    Can someone please provide the code that will delete all rows in column B that contain a cell with the word 'Total'?

    Thanks

  2. #2
    Registered User
    Join Date
    12-22-2003
    Location
    Texas
    Posts
    90
    Sub deltotal()

    Range("b1").Activate
    Set Var = ActiveCell
    For x = 1 To 65536
    On Error GoTo skp:
    g = Application.WorksheetFunction.Search("total", Var)
    Set Var = ActiveCell.Offset(1, 0)
    ActiveCell.EntireRow.Select
    Selection.Delete
    GoTo newvar
    skp:
    Var.Offset(1, 0).Activate
    Set Var = ActiveCell
    Resume
    newvar:
    Var.Activate
    Next
    End Sub
    Barrfly

  3. #3
    JE McGimpsey
    Guest

    Re: Delete rows with specific text

    One way:

    Public Sub DeleteRows()
    Const sLOOK As String = "Total"
    Dim rFound As Range
    Dim rDelete As Range
    Dim sFoundAddr As String
    Set rFound = Columns(2).Find( _
    What:=sLOOK, _
    LookIn:=xlValues, _
    LookAt:=xlPart, _
    MatchCase:=False)
    If Not rFound Is Nothing Then
    Set rDelete = rFound
    sFoundAddr = rFound.Address
    Do
    Set rFound = Columns(2).FindNext(After:=rFound)
    Set rDelete = Union(rDelete, rFound)
    Loop Until rFound.Address = sFoundAddr
    rDelete.EntireRow.Delete
    End If
    End Sub





    In article <MAYDAY.1q80uh_1118088325.951@excelforum-nospam.com>,
    MAYDAY <MAYDAY.1q80uh_1118088325.951@excelforum-nospam.com> wrote:

    > Can someone please provide the code that will delete all rows in column
    > B that contain a cell with the word 'Total'?


  4. #4
    Jim Thomlinson
    Guest

    RE: Delete rows with specific text

    Here is some code that is a little faster since it does not loop through each
    cell...

    Public Sub RemoveTotals()
    Dim rngToSearch As Range
    Dim wks As Worksheet
    Dim rngFound As Range

    Set wks = ActiveSheet
    Set rngToSearch = wks.Columns(2)

    Set rngFound = rngToSearch.Find("Total")
    If rngFound Is Nothing Then
    MsgBox "No Totals Found"
    Else
    Do
    rngFound.EntireRow.Delete
    Set rngFound = rngToSearch.FindNext
    Loop Until rngFound Is Nothing
    End If
    End Sub
    --
    HTH...

    Jim Thomlinson


    "MAYDAY" wrote:

    >
    > Hi,
    >
    > Can someone please provide the code that will delete all rows in column
    > B that contain a cell with the word 'Total'?
    >
    > Thanks
    >
    >
    > --
    > MAYDAY
    > ------------------------------------------------------------------------
    > MAYDAY's Profile: http://www.excelforum.com/member.php...o&userid=13548
    > View this thread: http://www.excelforum.com/showthread...hreadid=376890
    >
    >


  5. #5
    Jim Thomlinson
    Guest

    Re: Delete rows with specific text

    How much faster is it to Union everthing into one big range and delete that
    at the end as you have done as opposed to deleteing as you go as my code has
    done. Have you ever checked? (Note to MayDay... Use this code it should be
    the most efficient)
    --
    HTH...

    Jim Thomlinson


    "JE McGimpsey" wrote:

    > One way:
    >
    > Public Sub DeleteRows()
    > Const sLOOK As String = "Total"
    > Dim rFound As Range
    > Dim rDelete As Range
    > Dim sFoundAddr As String
    > Set rFound = Columns(2).Find( _
    > What:=sLOOK, _
    > LookIn:=xlValues, _
    > LookAt:=xlPart, _
    > MatchCase:=False)
    > If Not rFound Is Nothing Then
    > Set rDelete = rFound
    > sFoundAddr = rFound.Address
    > Do
    > Set rFound = Columns(2).FindNext(After:=rFound)
    > Set rDelete = Union(rDelete, rFound)
    > Loop Until rFound.Address = sFoundAddr
    > rDelete.EntireRow.Delete
    > End If
    > End Sub
    >
    >
    >
    >
    >
    > In article <MAYDAY.1q80uh_1118088325.951@excelforum-nospam.com>,
    > MAYDAY <MAYDAY.1q80uh_1118088325.951@excelforum-nospam.com> wrote:
    >
    > > Can someone please provide the code that will delete all rows in column
    > > B that contain a cell with the word 'Total'?

    >


  6. #6
    JE McGimpsey
    Guest

    Re: Delete rows with specific text

    Since deleting every row requires reindexing all the remaining rows, and
    deleting all at once apparently doesn't, it's variable. The gain in
    efficiency should increase in proportion to both hits and total rows. In
    a (very) informal test with 16000 rows and 1000 hits, deleting each row
    took about 6 times as long as deleting all at once.


    In article <0837CBCA-512E-4B10-8DA9-6BE0DB9416C4@microsoft.com>,
    "Jim Thomlinson" <jamest@tcgiRe-Move-This.com> wrote:

    > How much faster is it to Union everthing into one big range and delete that
    > at the end as you have done as opposed to deleteing as you go as my code has
    > done. Have you ever checked? (Note to MayDay... Use this code it should be
    > the most efficient)


  7. #7
    Jim Thomlinson
    Guest

    Re: Delete rows with specific text

    I knew it would be more efficient but that is a lot. I will go the extra
    distance in the future to create one big range to delete at the end...
    Thanks... It never ceases to amaze me how much info you can pick up from this
    forum!
    --
    HTH...

    Jim Thomlinson


    "JE McGimpsey" wrote:

    > Since deleting every row requires reindexing all the remaining rows, and
    > deleting all at once apparently doesn't, it's variable. The gain in
    > efficiency should increase in proportion to both hits and total rows. In
    > a (very) informal test with 16000 rows and 1000 hits, deleting each row
    > took about 6 times as long as deleting all at once.
    >
    >
    > In article <0837CBCA-512E-4B10-8DA9-6BE0DB9416C4@microsoft.com>,
    > "Jim Thomlinson" <jamest@tcgiRe-Move-This.com> wrote:
    >
    > > How much faster is it to Union everthing into one big range and delete that
    > > at the end as you have done as opposed to deleteing as you go as my code has
    > > done. Have you ever checked? (Note to MayDay... Use this code it should be
    > > the most efficient)

    >


  8. #8
    Registered User
    Join Date
    08-24-2004
    Posts
    14
    Thank you all!

  9. #9
    mark
    Guest

    RE: Delete rows with specific text

    > Here is some code that is a little faster since it does not loop through
    each
    > cell...



    I tested that on 10,000 rows where every other one said "Total". You're
    right, it's a lot faster than the other.

    the way I do that is by passing a formula into a routine... (acknowledged,
    my method would not work if there were not 1 free column at the far right of
    the spreadsheet).

    the del_rows subroutine accepts any formula... if it meets the condition, it
    evaluates to "", if it doesn't, it evaluates to 1.

    the rountine puts the formula in, copies it to the end of the data range,
    sorts by the result, and deletes the ones that met the delete condition.

    In the same test, that method took about half a second.

+ 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