图形对象更新而不跳过,vba

时间:2017-01-13 16:03:39

标签: vba

我有一个代码,例如名为MyClass的类,并且希望在每个步骤之后在excel工作簿中更新结果,并且动态更新某些图表和图形对象。 更新单元格,图表和图像的延迟是正常的,但我必须添加一个额外的暂停,否则它将跳过所有进程并跳转图形更新到代码执行结束。 我该如何处理意外暂停?

Public a As Double
Public b As Double
Public counter As Integer

Private Sub Class_Initialize()
    a = Range("A1")
    b = Range("B1")
    counter = 1
End Sub
Public Sub some_calculation()
    a = a + b
End Sub
Public Sub graphic_update()
    counter = counter + 1
    Cells(count, 1) = a
    Sheet1.Shapes("Picture 1").Rotation = Sheet1.Shapes("Picture 1").Rotation + 45
End Sub
Public Function pause(pause_time As Double)
    Start_Time = Timer
    Do
    DoEvents
    Loop Until (Timer - Start_Time) >= pause_time
End Function
Sub test()
    Dim obj1 As New MyClass
    For i = 1 To 10
        obj1.some_calculation
        obj1.graphic_update    ' if I had a kind of confirmation of previous line execution, which is cells update, to go to the next one, it would be just ok.
        obj1.pause (0.001)
    Next i
End Sub

1 个答案:

答案 0 :(得分:1)

由于您请求了DoEvents的示例 - 我会尝试这样的事情:

Public Sub graphic_update()
    counter = counter + 1
    Cells(count, 1) = a
    Sheet1.Shapes("Picture 1").Rotation = Sheet1.Shapes("Picture 1").Rotation + 45
    DoEvents '<<<
End Sub