Repeatedly update contents of a list box while maintaining user control

荒凉一梦 提交于 2019-12-25 17:48:05

问题


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.



来源:https://stackoverflow.com/questions/8807070/repeatedly-update-contents-of-a-list-box-while-maintaining-user-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!