I want to write a macro which deletes values from column if they are smaller than a specific value indicated somewhere else in the worksheet.
My try
Sub clearsmall()
Dim r As Range
num1 = Cells(7, 5).Value
For Each r In Selection
If r.Value < num1 Then
r.Clear
End If
Next
End Sub
the problem is that thois requires the range to be selected by the user. Hoever, I want to specify it in the macro - using something like
Set r = Range("C16:C92")
how would I need to change the code?
Sub clearsmall()
Dim cell As Range, r As Range
Set r= Range("C16:C92")
num1 = Cells(7, 5).Value
For Each cell In r
If cell.Value < num1 Then cell.Clear
Next
End Sub
You can also shorten it down to
Sub clearsmall()
Dim cell As Range
Num1 = Cells(7, 5).Value
For Each cell In Range("C16:C92")
If cell.Value < num1 Then cell.Clear
Next
End Sub
Finally, you may want to use ClearContents instead of Clear, to clear cell value only, which is faster. This will not touch formats
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.