+ Reply to Thread
Results 1 to 4 of 4

Conditional row deletion based on value in a row

  1. #1
    Craig
    Guest

    Conditional row deletion based on value in a row

    I am trying to create a macro that will delete all rows in a worksheet that
    have the value 0 in column B. The macro should only delete rows where the
    value equals 0, and not rows where the value contains 0 (for example, 10).

    Ideally I'd like to have this in a form that I could edit in the future, so
    that if down the road I wanted to change the macro to delete all rows where
    column F contained the value "Elephant", I could do so without starting from
    scratch.
    thank you,
    Craig

  2. #2
    Bob Umlas
    Guest

    Re: Conditional row deletion based on value in a row

    Try this (untested)
    Sub DeleteThem()
    For i=Range("B65536").end(xlup).row to 1 step -1
    if cells(i,2).value = 0 then rows(i).Delete
    Next
    End Sub

    Sub DeleteElephantfromF()
    For i=Range("F65536").end(xlup).row to 1 step -1
    if cells(i,6).value = "Elephant" then rows(i).Delete
    Next
    End Sub

    HTH
    Bob Umlas
    Excel MVP

    "Craig" <[email protected]> wrote in message
    news:[email protected]...
    > I am trying to create a macro that will delete all rows in a worksheet

    that
    > have the value 0 in column B. The macro should only delete rows where the
    > value equals 0, and not rows where the value contains 0 (for example, 10).
    >
    > Ideally I'd like to have this in a form that I could edit in the future,

    so
    > that if down the road I wanted to change the macro to delete all rows

    where
    > column F contained the value "Elephant", I could do so without starting

    from
    > scratch.
    > thank you,
    > Craig




  3. #3
    Chip Pearson
    Guest

    Re: Conditional row deletion based on value in a row

    Craig,

    Try code like the following:

    Dim LastRow As Long
    Dim RowNdx As Long
    LastRow = Cells(Rows.Count, "B").End(xlUp).Row
    For RowNdx = LastRow To 1 Step -1
    If Cells(RowNdx, "B").Value = 0 Then
    Rows(RowNdx).Delete
    End If
    Next RowNdx

    --
    Cordially,
    Chip Pearson
    Microsoft MVP - Excel
    Pearson Software Consulting, LLC
    www.cpearson.com




    "Craig" <[email protected]> wrote in message
    news:[email protected]...
    >I am trying to create a macro that will delete all rows in a
    >worksheet that
    > have the value 0 in column B. The macro should only delete rows
    > where the
    > value equals 0, and not rows where the value contains 0 (for
    > example, 10).
    >
    > Ideally I'd like to have this in a form that I could edit in
    > the future, so
    > that if down the road I wanted to change the macro to delete
    > all rows where
    > column F contained the value "Elephant", I could do so without
    > starting from
    > scratch.
    > thank you,
    > Craig




  4. #4
    Dave Peterson
    Guest

    Re: Conditional row deletion based on value in a row

    One way is to apply Data|Filter|Autofilter to that column.

    Then filtering to show the cells with the value you want to delete.

    Then deleting those visible rows.

    Record a macro when you do it and you'll have code you can modify.

    Craig wrote:
    >
    > I am trying to create a macro that will delete all rows in a worksheet that
    > have the value 0 in column B. The macro should only delete rows where the
    > value equals 0, and not rows where the value contains 0 (for example, 10).
    >
    > Ideally I'd like to have this in a form that I could edit in the future, so
    > that if down the road I wanted to change the macro to delete all rows where
    > column F contained the value "Elephant", I could do so without starting from
    > scratch.
    > thank you,
    > Craig


    --

    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