[英]Deleting values from column if they are smaller than a specific value
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.问题是 thois 需要用户选择范围。 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.最后,您可能希望使用 ClearContents 而不是 Clear 来仅清除单元格值,这样会更快。 This will not touch formats
这不会触及格式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.