2016-10-28 51 views
-1

有人可以用以下方式提供幫助 - 我想編寫一個宏,該列從列中刪除值,如果它們小於工作表中其他位置指定的指定值。用於清除單元格的宏

我嘗試

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要求的範圍要由用戶來選擇。 Hoever,我想在宏觀指定它 - 使用類似

集合R =範圍(「C16:C92」)

我怎麼會需要改變的代碼?

回答

1
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中,而不是清除,只清除單元格的值,這是更快。這不會觸摸格式

+0

@Timheinerz,你通過它嗎? – user3598756

相關問題