2014-01-11 37 views
0

我试图给我们一个宏来执行黎曼总和并将答案放在Excel电子表格中,但似乎无法使电子表格中的单元格自动更新改变细胞。关于在更改单元格时进行Excel宏自动更新的问题

有人可以帮我解决这个问题吗?

这里是我的代码:

Sub RecCoil() 

Dim l, radius, D, y, z, H, Temp, Temp1, Temp2, wires, Temp3, f1, f2, B_at_a, F_at_a, B_Avg, F_Avg, a, mass, u_s, I_rails, I_coil, Func, Func1, Func2 As Double 
Dim x1, x2, D_Exp, Steps, A_Exp As Integer 


l = Cells(1, 3) 
radius = Cells(10, 3) 
D = Cells(2, 3) 
H = Cells(3, 3) 
a = Cells(9, 3) 
mass = Cells(7, 3) 
u_s = Cells(8, 3) 
I_rails = Cells(4, 3) 
I_coil = Cells(5, 3) 

Steps = 10^4 
R = radius * Steps 
D_Exp = (D - radius) * (Steps/5) 
A_Exp = a * (Steps/10) 
Temp1 = 0 
Temp2 = 0 
Temp3 = 0 
Temp = 0 
Friction = mass * u_s * 9.81 
wires = H/radius 


For S = 0 To l * (Steps/10) 
    x1 = -S 
    x2 = (l * Steps/10) - S 
    Temp2 = 0 

    For j = R To D_Exp 
     y = 5 * j/Steps 
     Temp1 = 0 

     For k = -(wires/2) To (wires/2) 
      z = k 
      Temp = 0 

      For i = x1 To x2 
       x = i/Steps 
       f1 = x^2 + z^2 
       f2 = D - y 
       Func1 = (y/((y^2 + f1)^(3/2))) 
       Func2 = ((f2)/((f2^2 + f1)^(3/2))) 
       Func = Func1 + Func2 
       Temp = Temp + Func 
      Next i 
      Temp1 = Temp1 + Temp 

     Next k 
     Temp2 = Temp2 + Temp1 

    Next j 
    If (S = A_Exp) Then 
     B_at_a = (Temp2 * 10^-7 * I_coil) 
     Cells(1, 7) = B_at_a 
     F_at_a = (I_rails * D * B_at_a) - (Friction) 
     If (F_at_a > 0) Then 
      Cells(2, 7) = F_at_a 
     Else 
      Cells(2, 7) = 0 
     End If 
    End If 
    Temp3 = Temp3 + Temp2 

Next S 

B_Avg = (Temp3 * 10^-7 * I_coil)/(l * Steps) 
Cells(4, 7) = B_Avg 
F_Avg = (I_rails * D * B_Avg) - (Friction) 
If (F_Avg > 0) Then 
    Cells(5, 7) = F_Avg 
Else 
    Cells(5, 7) = 0 
End If 

End Sub 

Private Sub WorkSheet_Change(ByVal Target As Range) 

    Application.EnableEvents = False 
    Call RecCoil 
    Application.EnableEvents = True 

End Sub 

感谢您对所有您的帮助和耐心对这个问题:)

回答

1

尝试重命名第二个功能

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) 

此外,确保它在“ThisWorkbook”代码对象中。

0

关闭文件并重新启动它。 或者在您的直接窗口中键入application.enableevents = true(altF11和ctrl + G)。

我想你在RecCoil曾经的错误,并且改变事件做enableEvents方法=假,当你恢复,但仍处于假状态,所以没有事件可能发生......

另外,还要确保你不处于中断模式(在VBE中调试):点击停止按钮(正方形)。

如果仍然无法正常工作,您可以从开发人员功能区添加一个按钮,启动RecCoil。

0

非常感谢您的回复,但不幸的是,我没有办法让我的电子表格在更改单元格时更新。

有没有其他的事情我可以尝试?

相关问题