Deleting an entire row if the cell number value is within a certain range

rammi125

New Member
Joined
Jun 3, 2015
Messages
22
I am trying to write a macro in excel 2010 that will go through all the cells in column L which is filled with numbers, if the cell number is between -1 and 1 I want to delete the entire row.

Right now my macro is deleting everything except numbers that are between -1 and 1. When I switch the <> signs around it deletes everything. Can you help me know what the correct code is?


Sub Delete_Rows()

Dim rows As Range, cell As Range, value As Long

Set cell = Range("L2")
Do Until cell.value = ""
value = Val(cell.value)
If (value < -1 Or value > 1) Then
If rows Is Nothing Then
Set rows = cell.EntireRow
Else
Set rows = Union(cell.EntireRow, rows)
End If
End If
Set cell = cell.Offset(1)
Loop

If Not rows Is Nothing Then rows.Delete


End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Right now my macro is deleting everything except numbers that are between -1 and 1. When I switch the <> signs around it deletes everything. Can you help me know what the correct code is?
The problem is that when you switched them, the OR needed to become AND...

If value > -1 And value < 1 Then

I wasn't sure whether values of -1 and 1 should be deleted or not. The above code does not delete them. If you want to delete them also, change the > to >= and change the < to <=.
 
Upvote 0

Forum statistics

Threads
1,215,366
Messages
6,124,516
Members
449,168
Latest member
CheerfulWalker

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top