【问题标题】:Issues on making an excel macro auto update upon changing cells在更改单元格时进行 Excel 宏自动更新的问题
【发布时间】:2014-01-11 05:52:49
【问题描述】:

我正在尝试使用宏来执行黎曼和并将答案放在 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

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

【问题讨论】:

    标签: excel vba auto-update


    【解决方案1】:

    尝试将第二个函数重命名为

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

    另外,请确保它位于“ThisWorkbook”代码对象中。

    【讨论】:

      【解决方案2】:

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

      我认为您曾经在 RecCoil 中出现错误,并且更改事件执行 enableevents=false,当您恢复时,它仍处于 false 状态,因此不会发生任何事件...

      同时确保您没有处于中断模式(在 VBE 中调试):点击停止按钮(方形)。

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

      【讨论】:

        猜你喜欢
        • 2010-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-03
        • 2013-02-26
        • 2013-12-15
        • 2010-12-08
        相关资源
        最近更新 更多