+ Reply to Thread
Results 1 to 9 of 9

Macro to Delete Row based on <3

  1. #1
    Registered User
    Join Date
    03-16-2006
    Posts
    12

    Macro to Delete Row based on <3

    I have a spread sheet with data in A:A to G:G

    I want a macro that will check the value in G and if its less than 3 to delete that entire row and then move on to the next row and do the same

    So if G5 is 2 it deletes A5:G5 then moves onto the next row

    Any help appreciated

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Steve

    Try this (make sure you have a backup copy of your data in case something goes wrong)

    Sub DeleteRows()
    Dim lRow As Long
    Dim l4Row As Long

    'find last used row
    lRow = Cells.Find(what:="*", searchorder:=xlByRows, _
    searchdirection:=xlPrevious).Row
    'loop from last used row to 1st row
    'deleting rows as required
    For l4Row = 1 To lRow Step -1
    If Cells(l4Row, "g").Value < 3 Then
    Rows(l4Row).Delete
    End If
    Next l4Row
    End Sub

  3. #3
    Registered User
    Join Date
    03-16-2006
    Posts
    12
    I get the following error message when I try to run it

    object variable or With block variable not set

  4. #4
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243
    Hi Steve,

    Here's another option - I don't know which would be quicker, Mudraker's looping or the filtering below, give it a test & let us know.
    I've assumed row two is the first row of data (ie row one is a header row):

    Sub filterandDelete()
    Application.ScreenUpdating = False
    ActiveSheet.UsedRange.AutoFilter Field:=7, Criteria1:="<3"
    Range("a2", ActiveCell.SpecialCells(xlLastCell)).EntireRow.SpecialCells(xlCellTypeVisible).Delete Shift:=xlUp
    ActiveSheet.ShowAllData
    Application.Goto Reference:="R1C1", Scroll:=True ' to return focus to top left
    Application.ScreenUpdating = True
    End Sub

    This may cause an error if there are no cells less than three, if this is possible, let me know & an error trap can be built in.

    hth
    Rob Brockett
    NZ
    Always learning & the best way to learn is to experience...

  5. #5
    Ron de Bruin
    Guest

    Re: Macro to Delete Row based on <3

    hi Steve

    Look here for a few ways
    http://www.rondebruin.nl/delete.htm



    --
    Regards Ron de Bruin
    http://www.rondebruin.nl


    "Steve M" <[email protected]> wrote in message
    news:[email protected]...
    >
    > I get the following error message when I try to run it
    >
    > object variable or With block variable not set
    >
    >
    > --
    > Steve M
    > ------------------------------------------------------------------------
    > Steve M's Profile: http://www.excelforum.com/member.php...o&userid=32520
    > View this thread: http://www.excelforum.com/showthread...hreadid=534540
    >




  6. #6
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Steve

    sorry I reversed a couple of items int he for command it should have been'

    Sub DeleteRows()
    Dim lRow As Long
    Dim l4Row As Long

    'find last used row
    lRow = Cells.Find(what:="*", searchorder:=xlByRows, _
    searchdirection:=xlPrevious).Row
    'loop from last used row to 1st row
    'deleting rows as required
    For l4Row = lRow To 1 Step -1
    If Cells(l4Row, "g").Value < 3 Then
    Rows(l4Row).Delete
    End If
    Next l4Row
    End Sub

  7. #7
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243
    Hi Ron,

    With the variety of methods on your linked pages there must be one or two ways that are better (eg faster, have less impact on memory etc) than the others. Which one would you recommend?

    Curious Rob

    Rob Brockett
    NZ
    Always learning & the best way to learn is to experience...
    Quote Originally Posted by Ron de Bruin
    hi Steve

    Look here for a few ways
    http://www.rondebruin.nl/delete.htm



    --
    Regards Ron de Bruin
    http://www.rondebruin.nl


    "Steve M" <[email protected]> wrote in message
    news:[email protected]...
    >
    > I get the following error message when I try to run it
    >
    > object variable or With block variable not set
    >
    >
    > --
    > Steve M
    > ------------------------------------------------------------------------
    > Steve M's Profile: http://www.excelforum.com/member.php...o&userid=32520
    > View this thread: http://www.excelforum.com/showthread...hreadid=534540
    >

  8. #8
    Ron de Bruin
    Guest

    Re: Macro to Delete Row based on <3

    Depend what you want to do

    Looping give you more control but is slower
    If possible use the Filter option first

    --
    Regards Ron de Bruin
    http://www.rondebruin.nl


    "broro183" <[email protected]> wrote in message
    news:[email protected]...
    >
    > Hi Ron,
    >
    > With the variety of methods on your linked pages there must be one or
    > two ways that are better (eg faster, have less impact on memory etc)
    > than the others. Which one would you recommend?
    >
    > Curious Rob
    >
    > Rob Brockett
    > NZ
    > Always learning & the best way to learn is to experience...
    > Ron de Bruin Wrote:
    >> hi Steve
    >>
    >> Look here for a few ways
    >> http://www.rondebruin.nl/delete.htm
    >>
    >>
    >>
    >> --
    >> Regards Ron de Bruin
    >> http://www.rondebruin.nl
    >>
    >>
    >> "Steve M" <[email protected]> wrote
    >> in message
    >> news:[email protected]...
    >> >
    >> > I get the following error message when I try to run it
    >> >
    >> > object variable or With block variable not set
    >> >
    >> >
    >> > --
    >> > Steve M
    >> >

    >> ------------------------------------------------------------------------
    >> > Steve M's Profile:

    >> http://www.excelforum.com/member.php...o&userid=32520
    >> > View this thread:

    >> http://www.excelforum.com/showthread...hreadid=534540
    >> >

    >
    >
    > --
    > broro183
    > ------------------------------------------------------------------------
    > broro183's Profile: http://www.excelforum.com/member.php...o&userid=30068
    > View this thread: http://www.excelforum.com/showthread...hreadid=534540
    >




  9. #9
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243
    Thanks for the feedback Ron :-)

    Rob Brockett
    NZ
    Always learning & the best way to learn is to experience...

+ 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