Search code examples
excelvbalistboxpowerquery

Populate VBA ListBox with Power Query


I have a userform to control my inventory records. I use Power Query to obtain information from other department file then populate it in my form. I already set up the query.

To use that information I just need to refresh all the connection and put it in the workbook as a table.

When I refresh all the connection while using the userform, the ListBox populates with the old data.

I added the line ThisWorkbook.RefreshAll to refresh the connection before I clear the the ListBox and then populate it with data. If I run the code a second time, the ListBox will populate with new data.

Is there a way to populate the ListBox with new data without running the code twice?


Solution

  • If your query allows background refresh your code will use the old data, as the code is much faster than the query update.

    Find your Query Properties, and if 'Enable background refresh' is set to False (Microsoft use True as default).

    Then add the DoEvent command in your code just after you call for refresh all.

    Believe this will do the trick for you.