![](/img/trans.png)
[英]Excel VBA - Looping to count cell values larger than / smaller than specific value
[英]Deleting values from column if they are smaller than a specific value
我想编写一个宏,如果它们小于工作表中其他地方指示的特定值,则从列中删除值。
我的尝试
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
问题是 thois 需要用户选择范围。 但是,我想在宏中指定它 - 使用类似的东西
Set r = Range("C16:C92")
我需要如何更改代码?
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
您也可以将其缩短为
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
最后,您可能希望使用 ClearContents 而不是 Clear 来仅清除单元格值,这样会更快。 这不会触及格式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.