0

In VBA, I am repeatedly updating the contents of a list box with the text of a steadily growing text file. Is there any way to let the user maintain control (scroll the list box, press a button) while the loop that I'm in to update the list box is running? An alternative to application.wait would also work; as in, update -> wait x seconds (where the user can still do stuff -> update again -> repeat.

4

1 回答 1

1

这是一个开始。在常规模块中,粘贴以下代码:

Sub ShowUserForm()
UserForm1.Show vbModeless
End Sub

Sub UpdateTextBox()
Dim ws As Excel.Worksheet

Set ws = ThisWorkbook.Worksheets(1)
With UserForm1.ListBox1
    .List = ws.Range("A1:A" & ws.Range("A" & ws.Rows.Count).End(xlUp).Row).Value2
    .ListIndex = .ListCount - 1
End With
Application.OnTime Now + TimeValue("00:00:5"), "UpdateTextBox"
End Sub

在您的用户表单中,将此代码粘贴到 UserForm_Activate 事件中:

UpdateTextBox

它使用代码更新工作簿中 Sheet1 的 A 列中的值。

要对其进行测试,请运行 ShowUserForm。

于 2012-01-10T21:39:23.467 回答