【发布时间】:2018-04-18 23:20:01
【问题描述】:
上下文
我正在尝试制作一份轻松的多户家庭租赁分析工作表。最终用户对excel一无所知。一个部分的数据点是单位类型、每种类型的单位数量、单位月租金和单位年租金。我希望用户能够输入月租并计算年租,反之亦然(假设在这两种情况下,他们也输入了每种类型的单位数)。有问题的公式比您想象的要简单:
月租 = 年租 / 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
【问题讨论】:
-
如果您使用事件处理程序来响应更改,那么您还需要每月/每年的公式吗?当用户更改每月或每年的金额时,您可以直接更新该行的另一个单元格。
-
使用
And Len(.Formula) = 0 Then代替And .Formula Is Nothing Then。当单元格没有公式时,“公式”只是一个空字符串。 -
仅供参考:我想在工作表中使用公式来让用户查看工作表正在执行的计算,如果他们愿意的话。我知道工作表完美无缺,但我必须让其他人自己验证。
标签: vba excel excel-formula excel-2010 circular-reference