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.
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.
A line such as:
will get the last used row in column A (i.e., column 1).![]()
Dim LastRow as Long LastRow = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
Then,
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.![]()
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
Ben Van Johnson
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.
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 theicon 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!)
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
Thanks JBeaucaire that worked.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks