![](/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.