Vba #DIV/0!错误并将其替换为零
我正在尝试下面的代码来检测DIV/0错误,并将其替换为零或空单元格。但是**线给了我错误Vba #DIV/0!错误并将其替换为零,vba,excel,compiler-errors,Vba,Excel,Compiler Errors,我正在尝试下面的代码来检测DIV/0错误,并将其替换为零或空单元格。但是**线给了我错误 Dim Cell As Range Dim iSheet as Worksheet For Each iSheet In sheets(Array("Sheet1", "Sheet2", "Sheet3")) With iSheet For Each Cell In .UsedRange **If Cell.Value = CVErr(xlErrDiv0) T
Dim Cell As Range
Dim iSheet as Worksheet
For Each iSheet In sheets(Array("Sheet1", "Sheet2", "Sheet3"))
With iSheet
For Each Cell In .UsedRange
**If Cell.Value = CVErr(xlErrDiv0) Then Cell.Value = 0**
Next Cell
End With
Next iSheet
在将其与错误值进行比较之前,您似乎必须检查
cell.Value
是否为错误
If IsError(Cell.Value) Then If Cell.Value = CVErr(xlErrDiv0) Then Cell.Value = 0
所以你的代码变成了
Sub mm()
Dim Cell As Range
Dim iSheet As Worksheet
For Each iSheet In Sheets(Array("Sheet1", "Sheet2", "Sheet3"))
With iSheet
For Each Cell In .UsedRange
If IsError(Cell.Value) Then If Cell.Value = CVErr(xlErrDiv0) Then Cell.Value = 0
Next Cell
End With
Next iSheet
End Sub
另一种限制单元循环的可能性是:
Sub mm()
Dim Cell As Range
Dim iSheet As Worksheet
For Each iSheet In Sheets(Array("Sheet1", "Sheet2", "Sheet3"))
With iSheet
If WorksheetFunction.CountIf(.UsedRange, "#DIV/0!") Then ' if any Division error found in current sheet used range cells
For Each Cell In .UsedRange.SpecialCells(xlCellTypeFormulas, xlErrors) 'loop through error cells only
If Cell.Value = CVErr(xlErrDiv0) Then Cell.Value = 0
Next
End If
End With
Next
End Sub
在将其与错误值进行比较之前,您似乎必须检查
cell.Value
是否为错误
If IsError(Cell.Value) Then If Cell.Value = CVErr(xlErrDiv0) Then Cell.Value = 0
所以你的代码变成了
Sub mm()
Dim Cell As Range
Dim iSheet As Worksheet
For Each iSheet In Sheets(Array("Sheet1", "Sheet2", "Sheet3"))
With iSheet
For Each Cell In .UsedRange
If IsError(Cell.Value) Then If Cell.Value = CVErr(xlErrDiv0) Then Cell.Value = 0
Next Cell
End With
Next iSheet
End Sub
另一种限制单元循环的可能性是:
Sub mm()
Dim Cell As Range
Dim iSheet As Worksheet
For Each iSheet In Sheets(Array("Sheet1", "Sheet2", "Sheet3"))
With iSheet
If WorksheetFunction.CountIf(.UsedRange, "#DIV/0!") Then ' if any Division error found in current sheet used range cells
For Each Cell In .UsedRange.SpecialCells(xlCellTypeFormulas, xlErrors) 'loop through error cells only
If Cell.Value = CVErr(xlErrDiv0) Then Cell.Value = 0
Next
End If
End With
Next
End Sub
请参阅(根据@DisplayName的回答)。请参阅(根据@DisplayName的回答)。@shwetaagnihotri,有反馈吗?@shwetaagnihotri,有反馈吗?