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
如何将IFERRO应用于Excel中的所有单元格_Excel_Vba - Fatal编程技术网

如何将IFERRO应用于Excel中的所有单元格

如何将IFERRO应用于Excel中的所有单元格,excel,vba,Excel,Vba,我有许多单元格具有DIV/0!所以我需要设置IFERROR函数。是否有办法将此公式应用于所有单元格,而不是手动将公式放入每个单元格 我尝试了这个VBA代码,但我正在寻找更简单的东西 Sub WrapIfError() Dim rng As Range Dim cell As Range Dim x As String If Selection.Cells.Count = 1 Then Set rng = Selection If Not rng.HasFormula The

我有许多单元格具有DIV/0!所以我需要设置IFERROR函数。是否有办法将此公式应用于所有单元格,而不是手动将公式放入每个单元格

我尝试了这个VBA代码,但我正在寻找更简单的东西

Sub WrapIfError()
Dim rng As Range
Dim cell As Range
Dim x As String

  If Selection.Cells.Count = 1 Then
    Set rng = Selection
    If Not rng.HasFormula Then GoTo NoFormulas
  Else
      On Error GoTo NoFormulas
        Set rng = Selection.SpecialCells(xlCellTypeFormulas)
      On Error GoTo 0
  End If

  For Each cell In rng.Cells
    x = cell.Formula
    cell = "=IFERROR(" & Right(x, Len(x) - 1) & "," & Chr(34) & Chr(34) & ")"
  Next cell

Exit Sub

'Error Handler
NoFormulas:
  MsgBox "There were no formulas found in your selection!"

End Sub

有人能帮我吗?

也许这些版本中有一个更容易教

Sub apply_Error_Control()
    Dim cel As Range

    For Each cel In Selection
        If cel.HasFormula Then
            'option 1
            cel.Formula = Replace(cel.Formula, "=", "=IFERROR(", 1, 1) & ", """")"
            'option 2
            'cel.Formula = "=IFERROR(" & Mid(cel.Formula, 2) & ", """")"
        End If
    Next cel

End Sub
我提供了两种方法来将应用作为错误控制的“wapper”。要使用第二个选项,请注释第一个选项并取消注释第二个选项


选择一个或多个单元格,然后运行宏;通常,通过Alt+F8,然后从工作表中运行。

我觉得代码很好。有什么问题吗?这对我来说不是问题,但我想用一些简单的东西,教其他不懂编程基础的人应用它。我唯一可能想到的问题是x=Midcell.Formula,2而不是x=cell.Formula,以便从公式中删除任何前导的=。或者-更高级一点-您也可以使用:IifInStr1,cell.Formula,=>0,Midselection.Formula,2,cell.Formula.@jainashih-您的公式中似乎有更多的等号,例如=号,而不仅仅是起始号。在这些情况下,只需使用选项2或我编辑为选项1的调整后的替换即可。