Repeatedly update contents of a list box while mai

2019-08-16 16:26发布

问题:

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.

回答1:

Here's a start. In a regular module, paste this code:

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

In your userform paste this code in the UserForm_Activate event:

UpdateTextBox

It updates the values from column A of Sheet1 in the workbook with the code.

To test it, run ShowUserForm.