更新循环内的标签

时间:2016-10-03 11:18:45

标签: vba excel-vba excel

问题

我有一个表单在激活时运行循环处理项。表单的可视部分包含一个文本,该文本应显示" Processing X og Y items ",以显示循环的进度。问题是,循环不会更新可视部分,而是导致Excel在运行时停止响应。我该如何防止这种情况发生?

编辑:我可能想补充说,函数DataFunctions.getStatus()以及所有Process.*()函数都包含一些数据库(T-SQL)通信。

代码

Private Sub UserForm_Activate()
    Dim currentStatusCode As Long, _
        agCount As Long, _
        currentAg As String

    'Get array and find its size
    agArray = getAgArray()
    agCount = UBound(agArray) + 1

    'Loop through the list of items and process them one by one.
    For i = 0 To agCount - 1
        updateLabel i + 1, agCount  '<-- This is where the visual is updated
        currentAg = agArray(i)

        currentStatusCode = DataFunctions.getStatus(currentAg)

        If currentStatusCode = 100 Then
            Process.validate currentAg
            currentStatusCode = DataFunctions.getStatus(currentAg)
        End If


        If currentStatusCode = 101 Then
            Process.create currentAg
            currentStatusCode = DataFunctions.getStatus(currentAg)
        End If

        If currentStatusCode = 103 Then
            Process.createSomethingElse currentAg
            currentStatusCode = DataFunctions.getStatus(currentAg)
        End If

        If currentStatusCode = 104 Then
            Process.changeStatus currentAg
            'No need to get the new status here
        End If

    Next i      

End Sub

1 个答案:

答案 0 :(得分:3)

使用Me.Repaint会强制表单重绘自己,从而更新标签。如果您的例行程序显示“未响应”,则可能还需要添加DoEvents。很多。