Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.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 删除时重新恢复公式 上下文_Vba_Excel_Excel Formula_Excel 2010_Circular Reference - Fatal编程技术网

Vba 删除时重新恢复公式 上下文

Vba 删除时重新恢复公式 上下文,vba,excel,excel-formula,excel-2010,circular-reference,Vba,Excel,Excel Formula,Excel 2010,Circular Reference,我正在尝试制作一份无麻烦的多户租赁分析工作表。最终用户对excel一无所知。一个部分的数据点是单元类型、每种类型的单元数、每种单元的月租金和每种单元的年租金。我希望用户能够输入月租金并计算年租金,反之亦然(假设在这两种情况下,他们也输入了每种类型的单元数)。这些公式比你想象的要简单得多: 月租金=年租金/12/单位数量 年租金=月租金*单元数量*12 我启用了迭代计算,使我能够在每个租金列中保留一个公式,因为它们的计算相互依赖(相关公式)。这非常有效,但仅适用于第一个条目。如果用户输入了月度租金

我正在尝试制作一份无麻烦的多户租赁分析工作表。最终用户对excel一无所知。一个部分的数据点是单元类型、每种类型的单元数、每种单元的月租金和每种单元的年租金。我希望用户能够输入月租金并计算年租金,反之亦然(假设在这两种情况下,他们也输入了每种类型的单元数)。这些公式比你想象的要简单得多:

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

年租金=月租金*单元数量*12

我启用了迭代计算,使我能够在每个租金列中保留一个公式,因为它们的计算相互依赖(相关公式)。这非常有效,但仅适用于第一个条目。如果用户输入了月度租金数据,但现在希望输入年度数据,则月度计算公式将消失然而,我能够通过我粘贴在下面的
私有子工作表_Change(ByVal Target作为范围)
中的一些巧妙代码来解决这个问题。这很好用。无论何时更改其中一个,都会为另一个插入公式,覆盖用户输入——除非。。。其中一个公式被删除,而不是刚刚更改

问题 我找不到有关用户删除公式时如何插入公式的任何信息。例如:假设用户之前输入了月租信息,工作表自动计算了年租金。现在,当用户返回删除月租时,月租单元不会重置,它是空的。公式不再存在,当他们编辑年租金时,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
Private子工作表\u更改(ByVal目标作为范围)
'宏替换循环引用单元格中的公式
'这是允许用户在一个数据库中输入一段数据
'列,并让另一列自动计算,
'即使他们已经将数据输入到
”“算了。
'
'例如:用户可以输入月租金以获得
'年度租金计算或他们可以输入年度租金
“计算月租金(假设他们也有
'单位数量列中提供的单位数量)。这
'宏覆盖未使用列的单元格内容,允许
'用户可以输入月租金数字并查看年租金
'然后是指定年租金和月租金
'列覆盖以前输入的图形
暗淡的年租金,月为范围
Dim-cll作为变体
'动态设置感兴趣的范围,以允许用户
'任意添加行数为零
设置年租金=范围(“R2:R”和范围(“总租金”)。第1行)
设置月份=范围(“P2:P”和范围(“总租金”)。第1行)
'必须禁用事件侦听以防止无限循环
Application.EnableEvents=False
'处理上述范围的后续更改,特别是,
“重建圆形
对于Target.Cells中的每个cll
与cll
“为一个月制定一个持久的公式
如果不相交(cll,MonthlyRent),则不算什么_
和。偏移量(0,2)。公式1c1“=RC[-2]*12*RC[-11]”然后
.Offset(0,2).公式1c1=“=RC[-2]*12*RC[-11]”
“”在删除月份时恢复该月份
“否则不相交(cll,MonthlyRent)算不了什么_
那么,公式什么都不是
'
“.FormulaR1C1=“=IFERROR(RC[2]/12/RC[-9],0)”
“为年租金制定一个持久的公式
否则不相交(cll,年租金)算不了什么_
和.Offset(0,-2).公式1c1“=IFERROR(RC[2]/12/RC[-9],0)”然后
.Offset(0,-2)。公式1c1=“=IFERROR(RC[2]/12/RC[-9],0)”
“”删除时恢复年租金公式
'否则不相交(cll,年租金)算不了什么_
那么,公式什么都不是
'
“.公式R1C1=“=RC[-2]*12*RC[-11]”
如果结束
以
下一个cll
Application.EnableEvents=True
端接头
更好的方法(因为您已经在使用事件处理程序响应更改)可能是完全跳过公式,而当用户直接更改月度或年度金额时,只需更新该行的另一个单元格即可

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

而不是

 ... And .Formula Is Nothing Then 
使用


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

如果您使用事件处理程序响应更改,那么您甚至需要月度/年度公式吗?当用户更改月度或年度金额时,您可以直接更新该行的另一个单元格。而不是
和.Formula Is Nothing,然后使用
和Len(.Formula)=0,然后使用
。当单元格没有公式时,“公式”只是一个空字符串。仅供参考:如果用户愿意,我想在工作表中使用公式,以允许用户查看工作表正在执行的计算。我知道工作表是完美的,但我必须允许其他人自己验证它。
 ... And Len(.Formula) = 0 Then