在错误发生时,显示空白并继续到下一行 VBA 宏。

huangapple go评论75阅读模式
英文:

On error, show blank and resume to next row VBA MACRO

问题

我有一个非常简单的范围除法。

但是在出现错误时,应该是:如果单元格 D 为空,则 E 也应为空。
我尝试了长度(lens)和 iferror 或 isblank,似乎无法使其工作。

在错误发生时,显示空白并继续到下一行 VBA 宏。

Sub MyMacro()

Range("E9:E20").Formula = "=IF(D9<>"", 1/D9, "") 

End Sub
英文:

I have a very simple division for a range.

But on error, it should be: if cell D is empty, then E should also be empty.
I have tried lens and iferror or isblank and can't seem to get it to work.

在错误发生时,显示空白并继续到下一行 VBA 宏。

Sub MyMacro()

Range(&quot;E9:E20&quot;).Formula = (&quot;=1/D9&quot;)
    
End Sub

答案1

得分: 0

我没有看到上面的评论,但部分理解了。
搜索特定错误以清除,并将该错误设置为""。

然后,我创建了一个私有宏,在每次单元格更改时运行主要宏,以便在初始错误清除后,空白单元格现在可以重新添加公式。

注意:您必须将此放入特定的工作表,或者如果要将其放入模块中,还可以添加工作表目标。

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("P15:P134")) Is Nothing Then
        Call MyMacro
    End If
End Sub

Sub MyMacro()
    Dim Cell As Range
    
    Range("R15:R134").Formula = "=1/P15"
    
    For Each Cell In Range("R15:R134")
        If IsError(Cell.Value) Then
            If Cell.Value = CVErr(xlErrDiv0) Then
                Cell.Value = ""
            End If
        End If
    Next Cell
End Sub
英文:

I didn't see the above comment but figured it out partially.
Search for a specific error to clear, and set that error to "".

Then I created a private macro to run the main macro every time there is a cell change, so blank cells can now have the formula added back in after initial error clear.

Note: you must put this into the specific sheet, or add in worksheet targets if you want this in a module.

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range(&quot;P15:P134&quot;)) Is Nothing Then
Call MyMacro
End If
End Sub

Sub MyMacro()
    Dim Cell As Range
    
    Range(&quot;R15:R134&quot;).Formula = (&quot;=1/P15&quot;)
    
            For Each Cell In Range(&quot;R15:R134&quot;)
                If IsError(Cell.Value) Then If Cell.Value = CVErr(xlErrDiv0) Then Cell.Value = &quot;&quot;
            Next Cell
End Sub

答案2

得分: 0

我猜你要找的公式是=IFERROR(1/D9,"")

这将执行并返回除法结果,但如果结果出现错误(因为D9为空或不包含数字),它将返回一个空字符串。当然,你可以用任何你想要的值替代它。

如果你想通过VBA编写它,通常更容易切换到R1C1表示法:=IFERROR(1/RC[-1],"")

With ActiveSheet    ' 在你想要处理特定工作表时替换此部分
    Dim formula As String
    formula = "=IFERROR(1/RC[-1],"""")"
    .Range("E9:E20").FormulaR1C1 = formula
End With

更新(在澄清需要的内容之后)。看一下下面的代码。你肯定需要使用Worksheet_Change事件来处理用户输入。代码有两个部分:

  1. 如果在第一列输入了内容,右侧的单元格将获得该公式 - 但仅当它不包含固定值时。
  2. 如果第二列的值被删除,它将再次获得该公式。只需根据你的需求调整两个Intersect命令中的范围。
Private Sub Worksheet_Change(ByVal Target As Range)

    Const formula = "=IFERROR(1/RC[-1],"""")"
    Application.EnableEvents = False
    On Error GoTo Change_Exit
    
    ' 处理第一列
    Dim r As Range, cell As Range
    Set r = Intersect(Target, Range("D:D"))

    If Not r Is Nothing Then
        For Each cell In r
            Dim neighbor As Range
            Set neighbor = cell.Offset(0, 1)
            If neighbor.HasFormula Or IsEmpty(neighbor) Then
                Dim f As String
                neighbor.FormulaR1C1 = formula
            End If
        Next
    End If
    
    ' 处理第二列
    Set r = Intersect(Target, Range("E:E"))
    If Not r Is Nothing Then
        For Each cell In r
            If IsEmpty(cell) Then    ' 值已删除,写入公式
                cell.FormulaR1C1 = formula
            End If
        Next
    End If
    
Change_Exit:
    Application.EnableEvents = True
End Sub
英文:

I guess the formula you are looking for is =IFERROR(1/D9,&quot;&quot;)

This will execute and return the division, but if the result is any error (because D9 is empty or doesn't contain a number), it will return an empty string. Of course you can replace that with any value you want.

If you want to write that via VBA, it is often easier to change to R1C1-Notation: =IFERROR(1/RC[-1],&quot;&quot;)

With ActiveSheet    &#39; Replace this when you want to work on a specific sheet
    Dim formula As String
    formula = &quot;=IFERROR(1/RC[-1],&quot;&quot;&quot;&quot;)&quot; 
    .Range(&quot;E9:E20&quot;).FormulaR1C1 = formula
End With

Update (after clarifying what is needed).
Have a look to the following code. You will for sure need to handle the user input using the Worksheet_Change-Event. The code has 2 pieces:

If something was entered in the first column, the cell to the right will get the formula - but only if it doesn't contain a fixed value.

If a value on the second column was removed, it will get the formula again. Just adapt the ranges in the two Intersect-commands to your needs.

Private Sub Worksheet_Change(ByVal Target As Range)

    Const formula = &quot;=IFERROR(1/RC[-1],&quot;&quot;&quot;&quot;)&quot;
    Application.EnableEvents = False
    On Error GoTo Change_Exit
    
    &#39; Handle first column
    Dim r As Range, cell As Range
    Set r = Intersect(Target, Range(&quot;D:D&quot;))

    If Not r Is Nothing Then
        For Each cell In r
            Dim neighbor As Range
            Set neighbor = cell.Offset(0, 1)
            If neighbor.HasFormula Or IsEmpty(neighbor) Then
                Dim f As String
                neighbor.FormulaR1C1 = formula
            End If
        Next
    End If
    
    
    &#39; Handle second column
    Set r = Intersect(Target, Range(&quot;E:E&quot;))
    If Not r Is Nothing Then
        For Each cell In r
            If IsEmpty(cell) Then    &#39; Value was deleted, write formula
                cell.FormulaR1C1 = formula
            End If
        Next
    End If
    
Change_Exit:
    Application.EnableEvents = True
End Sub

huangapple
  • 本文由 发表于 2023年6月22日 13:51:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528912.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定