vba update listbox based on cell in Excel
Q165. In Excel, how can I update a listbox based on data in a list using VBA?
For example, I have a Activex listbox "Countries". I want to populate it with the names listed in named range "COUNTRIES_SUPPORTED".
To do it in Excel, here is the answer:
- Option Explicit
- Sub AddEntriestoListBox()
- Dim cell As Range
- For Each cell In Range("COUNTRIES_SUPPORTED")
- ActiveSheet.Countries.AddItem cell.Value
- Next cell
- End Sub
a) Line 5 - 7 is used to loop through all cells in the named range and then add each entry to ActiveX listBox.
Result after Macro execution: