2011-10-31 46 views
1

说我有下式:V = IR更新从公式的任何细胞在Excel 2010

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

例如,

  • 如果我给定的I和R中的值,我需要显示在单元V的结果,其中我有V(用户保持它0或空白来表示这就是他想要找出)

Excel工作表前值:

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

Excel工作表后:

+------+---------+------+ 
| V | I  | R | 
+------+---------+------+ 
| 10 | 1  | 10 | 
+------+---------+------+ 
  • 如果我给V和R的值,我需要在我有电池,以显示我的结果我(用户保持其0或空白,以表示这就是他想要的价值找出)

Excel工作表之前:

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

Excel工作表后:

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

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

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

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

我该如何做这项工作?

回答

1

我会给你一个关于如何解决这个问题的提示......:^)与其试图在单元格中输入公式,不如写一个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或其他)

请注意,这只是伪代码。

1

据我所知,没有Excel的解决方案。你将不得不使用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,和我,还有一个“通知”单元名称的细胞,这将提供有用的反馈给用户。如果你不想给他们名字,那么你可以使用例如"B2"而不是Voltage"等我也只检查空白单元格,而不是零值。我会把那部分留给你。

上述宏属于代码模块,必须手动运行。您可以在工作表上放置一个CommandButton,并在用户点击时使其运行宏。另外,如果你想让它自动每个用户改变输入时间运行,可以将表模块中放置此:

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 

这是我想到的即兴。多做一点工作就可以使它更好用,更方便用户使用。