CodexBloom - Programming Q&A Platform

VBA: How to efficiently clear and re-populate a ListBox without flickering in Excel UserForms?

πŸ‘€ Views: 94 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-05
vba excel userform

I'm having a hard time understanding I'm reviewing some code and I'm stuck on something that should probably be simple. I'm trying to update a ListBox in an Excel UserForm without causing flickering and performance hiccups. The ListBox is populated based on a selection made in another control, and I'm working with issues with screen flickering during the update process. I've attempted to use `Application.ScreenUpdating = False` before clearing and repopulating the ListBox, but it still seems to flicker, especially for larger datasets. Here’s the code snippet I’m using: ```vba Private Sub ComboBox1_Change() Dim i As Long Dim myData As Variant Dim listItems As Collection ' Turn off screen updating Application.ScreenUpdating = False ' Clear existing items ListBox1.Clear ' Assuming myData is an array containing the new items myData = Array("Item 1", "Item 2", "Item 3", "Item 4") Set listItems = New Collection ' Populate ListBox For i = LBound(myData) To UBound(myData) listItems.Add myData(i) Next i For i = 1 To listItems.Count ListBox1.AddItem listItems(i) Next i ' Turn screen updating back on Application.ScreenUpdating = True End Sub ``` I also tried using `DoEvents` at various points, but it didn't resolve the flickering. The ListBox might contain anywhere from 50 to 200 items based on user selection, so I'm looking for a more efficient way to handle the repopulation. Any suggestions on how to minimize flicker or improve performance in this scenario would be greatly appreciated! For context: I'm using Vba on Ubuntu 20.04. Hoping someone can shed some light on this. I'm open to any suggestions. Any suggestions would be helpful.