在Excel 2010中更新公式中的任何单元格

说我有公式: V = IR

在Excel中,我需要计算这个可能的任何组合。

例如,

  • 如果给予I和R的值,我需要在V中的单元格中显示V的结果(用户保持0或空白表示这是他想要查找的值)

Excel表格之前:

 +------+---------+------+ | V | I | R | +------+---------+------+ | 0 | 1 | 10 | +------+---------+------+ 

Excel表后:

 +------+---------+------+ | V | I | R | +------+---------+------+ | 10 | 1 | 10 | +------+---------+------+ 
  • 如果给定V和R的值,我需要在I中的单元格中显示I的结果(用户保持0或空白表示这是他想要查找的值)

Excel表格之前:

 +------+---------+------+ | V | I | R | +------+---------+------+ | 10 | 0 | 10 | +------+---------+------+ 

Excel表后:

 +------+---------+------+ | V | I | R | +------+---------+------+ | 10 | 1 | 10 | +------+---------+------+ 

编写一个函数来检查一个单元格是否为0,并更新另一个“答案”单元格(除了V,I,R;这个单元格显式地调用这个函数)并且没有问题。

挑战是除了额外的“答案”单元之外,还有空白单元格(我们需要从提供的其他variables中计算出的值)更新。

Excel不允许公式更新任何单元格 ,这将需要一些解决方法 – 这是我想不到的。

我该如何做这项工作?

我会给你一个关于如何解决这个问题的提示……:^)与其尝试在单元格中input公式,不如写一个VBA脚本,它能够计算出应用公式的三个排列中的哪一个给定V = IR,基于哪个单元格是空的。 所以你的逻辑的第一部分看起来像这样:

 if cellV == "" and cellI <> "" and cellR <> "" then cellV = cellI * cellR elseif cellV <> "" and cell I = "" and cellR <> "" then cellI = cellV/cellR elseif cellV <> "" and cell I<> "" and cellR = "" then cellR = cellV/cellR else alert("must provide at least two out of three values before evaluation"); 

并附上这个你喜欢的任何事件。 例如一个事件,检查是否指定了3个值中的2个(如onUpdate或其他)

请知道这只是伪代码。

据我所知,没有Excel的解决scheme。 你将不得不使用VBA。

这对我工作:

 Option Explicit Sub Electricity() Dim nEmptyCells As Integer nEmptyCells = -CInt(IsEmpty(Range("Voltage"))) _ - CInt(IsEmpty(Range("Current"))) _ - CInt(IsEmpty(Range("Resistance"))) If nEmptyCells = 1 Then If IsEmpty(Range("Voltage")) Then 'Calculate voltage Range("Voltage") = Range("Current") * Range("Resistance") ElseIf IsEmpty(Range("Current")) Then 'Calculate current Range("Current") = Range("Voltage") / Range("Resistance") ElseIf IsEmpty(Range("Resistance")) Then 'Calculate resistance Range("Resistance") = Range("Voltage") / Range("Current") End If Range("Notification") = "Calculation successful." ElseIf nEmptyCells > 1 Then Range("Notification") = "Insufficient input." ElseIf nEmptyCells < 1 Then Range("Notification") = "Too much input. Problem is overspecified." End If End Sub 

请注意,我给与V,R和I相对应的单元格命名,以及一个“Notification”单元格,它会给用户提供有用的反馈信息。 如果你不想给他们的名字,那么你可以只使用例如"B2"而不是Voltage"等我也只检查空白单元格,而不是零值,我会留下那部分给你。

上面的macros属于代码模块,必须手动运行。 您可以在工作表上放置一个CommandButton,并在用户点击时使其运行macros。 或者,如果您希望每当用户更改input时都自动运行,则可以将其放置在工作表模块中:

 Private Sub Worksheet_Change(ByVal Target As Range) If Target.Address = Range("Voltage").Address _ Or Target.Address = Range("Current").Address _ Or Target.Address = Range("Resistance").Address Then Electricity End If End Sub 

这是我想到的袖口。 多一点工作,可以使其更好,更方便用户。