Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba #DIV/0!错误并将其替换为零_Vba_Excel_Compiler Errors - Fatal编程技术网

Vba #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

我正在尝试下面的代码来检测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) 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,有反馈吗?