简体   繁体   English

如何使两个像元相互依赖而没有循环误差?

[英]How do I make two cells dependent on one another without circular error?

In Excel 2013, I have cells A1, A2, and A3 along with B1, B2, and B3. 在Excel 2013中,我具有单元格A1,A2和A3以及B1,B2和B3。 Column A is a yearly calculation and Column B is a weekly calculation. A列是年度计算,B列是每周计算。

For example, if the user updates A1 (eg, 5200 ), I want to auto-update B1 (eg, 100 ). 例如,如果用户更新A1(例如5200 ),我想自动更新B1(例如100 )。 If the user updates B1 (eg, 50 ), I want to auto-update A1 (eg, 2600 ). 如果用户更新B1(例如50 ),我想自动更新A1(例如2600 )。 Likewise for A2-B2 and A3-B3. 对于A2-B2和A3-B3同样。

When I try to monitor both, it creates a circular issue and Excel crashes. 当我尝试监视两者时,它会产生一个循环问题,并且Excel崩溃。

Don't use formula at all then. 那就不要使用公式了。 Use a _Change event like this: 使用_Change事件,如下所示:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Intersect(Target, Range("A:B")) Is Nothing Then Exit Sub

Application.EnableEvents = False  'Prevent infinite looping

If Target.Column = 1 Then
    'User has changed something in column A:
    Target.Offset(0, 1).Value = Target / 52
Else
    'User has changed something in column B:
    Target.Offset(0, -1).Value = Target * 52
End If

Application.EnableEvents = True
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Excel中将相邻单元格彼此配对? - How do I pair adjacent cells with one another in Excel? 如何使我的代码运行更快? 将单元格从一张纸复制到另一张纸 - How can I make my code running faster? Copy cells from one sheet to another 如何合并另一个工作表中的单元格? - How do I merge cells in another worksheet? 如何将两个单元格乘以它们的名称? - how do i multiply two cells by their names? 我怎么知道两个单元格是否合并? - How do I know if the two cells are merged? 如何将来自多个excel单元的数据合并到一个列表中,然后删除重复项? - How do I join the data from multiple excel cells to make one list and then remove the duplicates? VBA excel:如何在不选择的情况下将单元格中的数据作为数组获取到同一列中的一行? - VBA excel: How do I get data in cells as an array up one row in the same column without selecting? 如何根据另一列中的所有单元格超过 0 来获得一列的总数 - How do I get the total of one column based on all cells in another column being over 0 考虑多种条件,如何将一列中的单元格与另一列中的单元格匹配? - How do I match cells in one column to another, considering multiple conditions? 如何使用 Excel VBA 从两个工作簿中复制特定单元格并将它们合并为一个? - How do I use Excel VBA to copy specific cells from two workbooks and combine them into one?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM