Clear cell Contents if condition is met

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
I need a script to clear contents of cells in a range that may contain values less than 1.

The data starts from A7 and I am clearing from the range D7 to N7 down to the last used row.

Thanks
Kelly
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
try this out

Code:
Sub ClearLessThanOne()
Dim lastRow As Integer
Dim r As Range
With ActiveSheet
    lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
For Each r In Range("D7:N" & lastRow)
    If r.Value < 1 Then
        r.Value = ""
    End If
Next

End Sub
 
Upvote 0
Code:
Sub Clear_Contents()

Dim c As Range


Application.ScreenUpdating = False


For Each c In Range(Cells(7, 1), Cells(Rows.Count, 1).End(xlUp))
    If c.Value < 1 Then Range(Cells(c.Row, 4), Cells(c.Row, 14)).ClearContents
Next c


Application.ScreenUpdating = True


End Sub
 
Upvote 0
try this out

Code:
Sub ClearLessThanOne()
Dim lastRow As Integer
Dim r As Range
With ActiveSheet
    lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
For Each r In Range("D7:N" & lastRow)
    If r.Value < 1 Then
        r.Value = ""
    End If
Next

End Sub


Great!!!

Working just cool

Thanks
 
Upvote 0
Code:
Sub Clear_Contents()

Dim c As Range


Application.ScreenUpdating = False


For Each c In Range(Cells(7, 1), Cells(Rows.Count, 1).End(xlUp))
    If c.Value < 1 Then Range(Cells(c.Row, 4), Cells(c.Row, 14)).ClearContents
Next c


Application.ScreenUpdating = True


End Sub


Nothing happens
I have used Sheet1.Range(
 
Upvote 0
I would think this one-liner should work for you...
Code:
[table="width: 500"]
[tr]
	[td]Sub ClearLessThanOne()
  Range("A7", Cells(Rows.Count, "A").End(xlUp)).Replace "0.*", "", xlWhole, , , , False, False
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
I would think this one-liner should work for you...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub ClearLessThanOne()
  Range("A7", Cells(Rows.Count, "A").End(xlUp)).Replace "0.*", "", xlWhole, , , , False, False
End Sub[/TD]
[/TR]
</tbody>[/TABLE]

It did not clear the cells. I just don't know why
 
Upvote 0
Rick,

I think it should be
Code:
Sub ClearLessThanOne()
  Range("[COLOR=#FF0000]D7:N[/COLOR]" & Cells(Rows.Count, "A").End(xlUp).Row).Replace "0.*", "", xlWhole, , , , False, False
End Sub
 
Upvote 0
Rick,

I think it should be
Code:
Sub ClearLessThanOne()
  Range("[COLOR=#FF0000]D7:N[/COLOR]" & Cells(Rows.Count, "A").End(xlUp).Row).Replace "0.*", "", xlWhole, , , , False, False
End Sub
Yes, you are correct... thanks for catching that. As it turns out, I simply did not read the original question thoroughly enough.:oops:
 
Last edited:
Upvote 0
The OP states clear values less than 1. The OP didn't say that all values were greater than zero. Rick's one-liner will not clear negative values.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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