2015-09-27 151 views
0

我正在嘗試在Excel中清除A1:A100單元格的宏,但我只找到一個可以對一個單元格執行此操作的宏。我怎樣才能讓它爲更多的細胞工作?如何通過宏清除Excel中的單元格

Sub ClearCell() 
Dim Rng As Range 
Set Rng = ActiveSheet.Range("A1") 

Dim i As Long 
For i = 1 To 10 
    If Rng.Cells(i,1) = "" Then 
     Rng.Cells(i,1).ClearContents 
    End If 
Next i 
End Sub 
+0

谷歌*使用範圍的使用*。或嘗試類似「Rng.Cells(」A1:A100「)。ClearContents' ... –

回答

0

試試這個:

Sub test() 
Dim Cl As Range 
For Each Cl In [A1:A100] 
    If Cl.Value = "" Then 
     With Cl 
      .ClearFormats 
      .ClearContents 
     End With 
    End If 
Next Cl 
End Sub 
+0

它沒有工作:/ – Wallis

+0

你到底需要什麼?你想要刪除單元格格式還是隻想清除單元格內容? – Vasily

+0

已經更新了'.ClearFormats',這是你要求的嗎? – Vasily

0

更好的,試試這個:

Sub foo() 
    Range("A1:A100").ClearContents 
    Range("A1:A100").ClearFormats 
End Sub 
+0

OP不需要清除整個範圍,只需清除「A1:A100」範圍內空單元的內容 – Vasily

相關問題