删除后重新启用公式

时间:2018-04-18 23:20:02

标签: vba excel-vba excel-formula excel-2010 circular-reference

上下文

我正在努力制作一个无忧无虑的多家庭租赁分析工作表。最终用户对excel一无所知。一个部分的数据点是单位类型每种类型的单位数每单位月租每单位年租金< / em>的。我希望用户能够输入月租并计算年租金,反之亦然(假设,在这两种情况下,他们也进入了< em>每种类型的单位数。有问题的公式比你想象的更简单:

每月租金 = 年租 / 12 / 单位数量

年租 = 每月租金 * 单位数 * 12

我启用了迭代计算,允许我在每个租金列中保留一个公式,因为它们相互依赖于计算(依赖公式)。这很好用,但只适用于第一个条目。如果用户输入了月租数据,但现在想要输入年度数据,则每月计算的公式就会消失。 然而,我能够通过下面粘贴的Private Sub Worksheet_Change(ByVal Target As Range)中的一些狡猾代码来解决这个问题。这非常有效。每当更改一个公式时,将为另一个公式插入公式,覆盖用户输入 - 除非 ...其中一个公式被删除而不是仅仅更改。

问题

当用户删除公式时,我找不到有关如何插入公式的任何信息。例如:假设用户之前已输入月租的信息,工作表会自动计算年租金。现在,当用户返回删除月租时,月租单元不会重置,它是空的。公式不再存在,当他们编辑年租金时,VBA不会恢复月租金公式。

我不知道:

  • 为什么注释掉的ElseIf块不起作用

  • 为什么有时删除输入其中一个依赖单元格的信息会清除另一个依赖单元格,有时它不会

  • 为什么从一个公式中删除公式并编辑另一个公式不会恢复已删除的公式。例如。删除每月租金然后编辑年租金不会重新创建月租金公式

当用户删除其中一个从属公式时,理想的行为是让单元格返回到它们的起始位置,同时使用两个循环公式。它似乎只是重新输入公式,当它被删除应该做的伎俩,但令人惊讶的是,我找不到任何帮助如何做到这一点。

Private Sub Worksheet_Change(ByVal Target As Range)
'Macro replaces formulas in circular reference cells
'This is to allow users to enter a piece of data in one
'column and have the other column automatically calculate,
'even if they had already entered data into the cell that
'calculates.
'
'FOR EXAMPLE: users can enter the monthly rent to have the
'annual rent calculate OR they can enter the annual rent to
'have the monthly rent calculate (assuming they have also
'provided number of units in the No. of Units column). This
'macro overwrites the cell contents of the unused column, allowing
'users to enter a monthly rent figure and see what the annual rent
'is but then to specify the annual rent and have the monthly rent
'column overwrite their previously entered figure

Dim AnnualRent, MonthlyRent As Range
Dim cll As Variant

'Dynamically set the ranges of interest, to allow users to
'add rows willy-nilly
Set AnnualRent = Range("R2:R" & Range("Total_Ann_Rent").Row - 1)
Set MonthlyRent = Range("P2:P" & Range("Total_Ann_Rent").Row - 1)

'It is necessary to disable event listening to prevent an infinite loop
Application.EnableEvents = False

'Handle subsequent changes to the ranges set above, specifically,
'to rebuild the circularity
For Each cll In Target.Cells
    With cll
        'Make a persistent formula for MonthlyRent
        If Not Intersect(cll, MonthlyRent) Is Nothing _
            And .Offset(0, 2).FormulaR1C1 <> "=RC[-2]*12*RC[-11]" Then

            .Offset(0, 2).FormulaR1C1 = "=RC[-2]*12*RC[-11]"

'       'Reinstate the MonthlyRent when it's deleted
'        ElseIf Not Intersect(cll, MonthlyRent) Is Nothing _
'            And .Formula Is Nothing Then
'
'            .FormulaR1C1 = "=IFERROR(RC[2]/12/RC[-9],0)"


        'Make a persistent formula for Annual Rent
        ElseIf Not Intersect(cll, AnnualRent) Is Nothing _
            And .Offset(0, -2).FormulaR1C1 <> "=IFERROR(RC[2]/12/RC[-9],0)" Then

            .Offset(0, -2).FormulaR1C1 = "=IFERROR(RC[2]/12/RC[-9],0)"

'        'Reinstate Annual Rent formula when it's deleted
'        ElseIf Not Intersect(cll, AnnualRent) Is Nothing _
'            And .formula Is Nothing Then
'
'            .Formula R1C1 = "=RC[-2]*12*RC[-11]"

        End If
    End With
Next cll

Application.EnableEvents = True

End Sub

1 个答案:

答案 0 :(得分:1)

更好的方法(因为您已经使用事件处理程序来响应更改)可能是完全跳过公式,而是当用户直接更改月度或年度数量时,只需更新该行上的其他单元格。

或者,如果您想继续使用当前的方法:

而不是

    DataProvider.getPeople(
            true,
            results =
    )

使用

 ... And .Formula Is Nothing Then 

当一个单元格没有公式时,“公式”只返回一个空字符串。