宏正在运行时,VBA用户表单是“无响应”

时间:2015-06-30 23:54:34

标签: vba userform

为什么我的用户窗体在运行以下代码时显示“无响应”?我一直试图解决它,但尚未解决。

实际上它有时会起作用。我认为这个问题与屏幕更新有关。

enter image description here

' The input button in Sheet1
Sub Rectangle1_Click()

    'Remember time when macro starts
    StartTime = Timer

    ' To improve speed and performance
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual

    ' Show the userform
    UserForm1.Show vbModeless
    UserForm1.Label1.Caption = "Calculation in progress ... " & vbNewLine & "Please be patient"
    UserForm1.Label1.Font.Size = 12
    UserForm1.Top = (Application.Height / 2) - (UserForm1.Height / 2)
    UserForm1.Left = (Application.Width / 2) - (UserForm1.Width / 2)
    UserForm1.CommandButton1.Visible = False
    UserForm1.Repaint

    Call Insert_RawData

    'Determine how many seconds code took to run
    SecondsElapsed = Round(Timer - StartTime, 2)
    UserForm1.Label1.Caption = "This code ran successfully in " & SecondsElapsed & " seconds"
    UserForm1.CommandButton1.Visible = True

    ' Return back to the original settings
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.Calculation = xlCalculationSemiautomatic

End Sub

1 个答案:

答案 0 :(得分:1)

这可能是一些事情。如果你提到这个问题是间歇性的,那么程序很可能正在运行,而Windows只是标记它没有响应,因为程序工作太难以响应操作系统。

可能的问题可能是以下内容中的一个或组合:

  • Insert_RawData
  • 处理的数据量
  • 宏运行时打开的工作簿中的数据量
  • Insert_RawData由于其使用的其中一个单元格中的错误/未处理值而遇到错误(不太可能)

尝试缩小来源的一些建议:

  • 如果有一种方法可以持续运行以便在Excel窗口中显示“无响应”,请在调用Insert_RawData时插入一个断点并观察它是否运行以查看是否出现错误
    • 或者,尝试对Insert_RawData进行一些错误检查,并对处理错误检查失败的情况的代码进行断点
  • 在宏的每次运行期间捕获处理的数据量(字节,单元格,最简单的)以及运行时间&看看你的命中是否存在阈值(例如< = 1 GB运行良好,但是> 1GB且应用程序看起来已冻结)

除非您在Insert_RawData中遇到错误,否则宏可能会完成,这可能需要很长时间。

相关问题