<listbox name>.Clear
'if clearing a userform's listbox from outside the userform
<userform name>.<listbox name>.Clear
'example formHome.lstOutput.Clear
<listbox name>.Clear
'if clearing a userform's listbox from outside the userform
<userform name>.<listbox name>.Clear
'example formHome.lstOutput.Clear
<listbox name>.AddItem <value to be added>
'example - userform called formHome exists formHome.lstGuests.AddItem "Tristan Smith"
#'i' is an integer variable that holds the index For i = 0 To <list box name>.ListCount - 1 If <list box name>.Selected(i) = True Then Exit For Next i
#example For i = 0 To lstGuestList.ListCount - 1 If lstGuestList.Selected(i) = True Then Exit For Next i
<listbox name>.AddItem Sheets("<sheet name>").Cells(<row>, <column num>).Value & ...etc
#example - reads a list of food entries from a sheet Private Sub loadListbox() lstFoods.Clear lastEntryRow = Sheets("Foods").Cells(Sheets("Foods").Rows.Count, "A").End(xlUp).Row 'how many foods are there? For i = 2 To lastEntryRow 'adds each food entry row into the listbox lineDisplay = Sheets("Foods").Cells(i, 1).Value & Chr(9) & Sheets("Foods").Cells(i, 2).Value lstFoods.AddItem lineDisplay Next End Sub
lastEntryRow = Sheets("<sheet name>").Cells(Sheets("<sheet name>").Rows.Count, "<column letter>").End(xlUp).Row For i = 2 To lastEntryRow 'adds each row to listbox lineDisplay = Sheets("<sheet name>").Cells(i, <column you want to show in listbox>).Value & ...etc <listbox name>.AddItem lineDisplay Next
#example - reads a list of food entries from a sheet Private Sub loadListbox() lstFoods.Clear lastEntryRow = Sheets("Foods").Cells(Sheets("Foods").Rows.Count, "A").End(xlUp).Row 'how many foods are there? For i = 2 To lastEntryRow 'adds each food entry row into the listbox lineDisplay = Sheets("Foods").Cells(i, 1).Value & Chr(9) & Sheets("Foods").Cells(i, 2).Value lstFoods.AddItem lineDisplay Next End Sub
Chr(9)
#example - reads a list of food entries from a sheet Private Sub loadListbox() lstFoods.Clear lastEntryRow = Sheets("Foods").Cells(Sheets("Foods").Rows.Count, "A").End(xlUp).Row 'how many foods are there? For i = 2 To lastEntryRow 'adds each food entry row into the listbox lineDisplay = Sheets("Foods").Cells(i, 1).Value & Chr(9) & Sheets("Foods").Cells(i, 2).Value lstFoods.AddItem lineDisplay Next End Sub
<listbox name>.AddItem Sheets("<sheet name>").Cells(<row>, <column num>).Value
#example lineDisplay = Sheets("Foods").Cells(1, 1).Value & Chr(9) & Sheets("Foods").Cells(1, 2).Value lstFoods.AddItem lineDisplay
With <combobox menu’s name> .AddItem “option 1” .AddItem “option 2” … End With
'method 1
With <combobox name>
.AddItem “<option 1>”
.AddItem “<option 2>”
…
End With
'method 2 - populate from a range
<combobox name>.List = Sheets("<sheet name containing range>").Range("<range index>").Value