+ Reply to Thread
Results 1 to 6 of 6

Delete row based on Cell value

Hybrid View

  1. #1
    Registered User
    Join Date
    05-02-2009
    Location
    Eagan, MN
    MS-Off Ver
    Excel 365
    Posts
    60

    Delete row based on Cell value

    I am trying to write a macro that will go through my inventory sheet and delete all rows that have a value of zero or less in column F. The worksheet changes in size every week and I need it to adjust to the overall size.
    Last edited by MSchibs; 03-04-2010 at 01:25 AM.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,943

    Re: Delete row based on Cell value

    A line such as:
    Dim LastRow as Long
    LastRow = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
    will get the last used row in column A (i.e., column 1).
    Then,
    Dim TestCell  As Range, Ndx As Long
    For Ndx= LastRow to 1 Step -1
       If Cells(Ndx, [test column number here] = some test value Then
            ..etc.
       Next Ndx
    Will start the test from the BOTTOM of the range. Because moving from the top down, the row below a deleted row will move up to its place and thus be skipped when Ndx increments to the next value.
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    05-02-2009
    Location
    Eagan, MN
    MS-Off Ver
    Excel 365
    Posts
    60

    Re: Delete row based on Cell value

    I am using this code, but I need it to delete everything that is zero or less.

    Sub Delete_Rows()
    Dim rng As Range, cell As Range, del As Range
    Set rng = Intersect(Range("F:F"), ActiveSheet.UsedRange)
    For Each cell In rng
    \If (cell.Value) = "0" _
    Or (cell.Value) = "-1" Then
    If del Is Nothing Then
    Set del = cell
    Else: Set del = Union(del, cell)
    End If
    End If
    Next cell
    On Error Resume Next
    del.EntireRow.Delete
    End Sub
    Last edited by MSchibs; 03-04-2010 at 01:20 AM.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Delete row based on Cell value

    How about turning on the Data > Filter > AutoFilter and doing a Custom filter in column F for less than or equal to zero?

    That would show you all the items all at once, hiding the others. You could then delete all the visible rows, then turn off the AutoFilter and you're left with the rest.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Delete row based on Cell value

    The same suggestion in a macro format:
    Sub FilterDeleteColumnF()
    Dim Lastrow As Long
        
    Range("F:F").AutoFilter Field:=1, Criteria1:="<=0"
    Lastrow = Range("F" & Rows.Count).End(xlUp).Row
    
    If Lastrow > 2 Then Range("A2:A" & Lastrow).SpecialCells(xlCellTypeVisible).EntireRow.Delete xlShiftUp
    
    ActiveSheet.AutoFilterMode = False
    End Sub

  6. #6
    Registered User
    Join Date
    05-02-2009
    Location
    Eagan, MN
    MS-Off Ver
    Excel 365
    Posts
    60

    Re: Delete row based on Cell value

    Thanks JBeaucaire that worked.

+ 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