用0replace范围内的空白单元格

我试图用数字0replace范围内的所有空单元格。这是我的代码到目前为止,任何帮助我做错了将不胜感激。

range("A1:O1").Select range(Selection, Selection.End(xlDown)).Select If range("A1:O1").Value = "" Then range("A1:O1").Value = 0 End If 

使用循环

 Sub marine() Dim r As Range, rr As Range Set r = Range("A1:O1") Set r = Range(r, r.End(xlDown)) For Each rr In r If rr.Value = "" Then rr.Value = 0 Next rr End Sub 

你可以使用Range对象的SpecialCells()方法:

 if worksheetfunction.CountBlank(range("A1:O1"))>0 then range("A1:O1").specialcells(xlCellTypeBlanks).Value=0 

要么

 With Range("A1:O1") if worksheetfunction.CountBlank(.Cells) >0 Then .SpecialCells(xlCellTypeBlanks).Value=0 End With 

你的问题是你需要所有的单元格为空,以使条件成立。 相反,您可以定义一个范围并遍历范围的所有元素。

 Sub tmp() Dim r As Range, cell As Range Set r = Range(Range("A1:O1"), Range("A1").End(xlDown)) For Each cell In r If cell.Value = "" Then cell.Value = 0 Next cell End Sub