我正在尝试使用宏来执行Riemann总和并将答案放在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
感谢您对此问题的所有帮助和耐心:)
答案 0 :(得分:1)
尝试将第二个功能重命名为
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
另外,请确保它位于“ ThisWorkbook ”代码对象中。
答案 1 :(得分:0)
关闭文件并重新启动它。 或者只需在您的即时窗口(altF11和ctrl + G)中键入application.enableevents = true。
我认为您曾经在RecCoil中出现错误,并且更改事件正在执行enableevents = false,当您恢复时,它仍处于错误状态,因此不会发生任何事件...
同时确保您没有处于Break模式(在VBE中调试):点击Stop按钮(方块)。
如果仍然无法正常工作,您可以从开发人员功能区添加一个按钮,启动RecCoil。