Содержание
- 1 Add items to ListBox
- 2 Add item to List Box
- 3 Add names of all open workbooks to the list box
- 4 Assign the data in a worksheet to RowSource of a ListBox
- 5 Determining the selected item
- 6 Evaluating Which Items Are Selected in the Multiselect List Box
- 7 Get all selected items in a list box
- 8 Get selected from ListBox
- 9 Get the selected items in a ListBox
- 10 Make sure the RowSource property is empty
- 11 Select the items programmatically
Add items to ListBox
<source lang="vb">
Sub ShowDialog()
With UserForm1.ListBox1
.MultiSelect = fmMultiSelectSingle
.RowSource = ""
.AddItem "January"
.AddItem "February"
.AddItem "March"
.AddItem "April"
.AddItem "May"
.AddItem "June"
.AddItem "July"
.AddItem "August"
.AddItem "September"
.AddItem "October"
.AddItem "November"
.AddItem "December"
End With
UserForm1.Show
End Sub
</source>
Add item to List Box
<source lang="vb">
Private Sub Form_Load()
Dim obj As AccessObject
For Each obj In CurrentData.AllTables
Me.yourListBox.AddItem obj.Name
Next obj
End Sub
</source>
Add names of all open workbooks to the list box
<source lang="vb">
Sub UserForm_Initialize()
Dim wkBook As Workbook
For Each wkBook In Workbooks
lstWorkbooks.AddItem wkBook.Name
Next
End Sub
</source>
Assign the data in a worksheet to RowSource of a ListBox
<source lang="vb">
Private Sub obMonths_Click()
ListBox1.RowSource = "Sheet1!Months"
End Sub
</source>
Determining the selected item
<source lang="vb">
Private Sub OKButton_Click()
Dim Msg As String
Msg = "You selected item # "
Msg = Msg & ListBox1.ListIndex
Msg = Msg & vbNewLine
Msg = Msg & ListBox1.Value
MsgBox Msg
Unload UserForm1
End Sub
</source>
Evaluating Which Items Are Selected in the Multiselect List Box
<source lang="vb">
Private Sub cmdRunReports_Click()
Dim varItem As Variant
Dim lst As ListBox
Set lst = Me.yourList
If lst.MultiSelect > 0 Then
If lst.ItemsSelected.Count > 0 Then
For Each varItem In lst.ItemsSelected
DoCmd.OpenReport lst.ItemData(varItem), acViewPreview
Next varItem
End If
End If
End Sub
</source>
Get all selected items in a list box
<source lang="vb">
Private Sub OKButton_Click()
If ListBox1.ListIndex = -1 Then
msg = "Nothing"
Else
msg = ""
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) Then _
msg = msg & ListBox1.List(i) & vbCrLf
Next i
End If
MsgBox "You selected: " & vbCrLf & msg
Unload Me
End Sub
</source>
Get selected from ListBox
<source lang="vb">
Private Sub OKButton_Click()
ActiveCell = ListBox1.Value Unload Me
End Sub
</source>
Get the selected items in a ListBox
<source lang="vb">
Private Sub OKButton_Click()
Dim Msg As String
Dim i As Integer
Msg = "You selected" & vbNewLine
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) Then
Msg = Msg & ListBox1.List(i) & vbNewLine
End If
Next i
MsgBox Msg
Unload UserForm1
End Sub
</source>
Make sure the RowSource property is empty
<source lang="vb">
Sub ShowDialog1()
With UserForm1
.ListBox1.RowSource = "Sheet1!Months"
.obMonths.Value = True
End With
UserForm1.Show
End Sub
</source>
Select the items programmatically
<source lang="vb">
Private Sub SelectAllButton_Click()
For r = 0 To ListBox1.ListCount - 1
ListBox1.Selected(r) = True
Next r
End Sub
</source>
I need to get all the list-items of a listbox not only selected.
For example I have a list box with following list item:
A
C
K
L
How I can show them all in Immediate window?
Deduplicator
44.3k7 gold badges65 silver badges115 bronze badges
asked Jan 25, 2015 at 15:15
2
You can loop through all by using the column property of a ListBox. This Column(index, row) property return the value of the ListBox item at a specific index and row. For your simple ListBox the index will be 0. However, the row must be variable. For example you can use a for loop to get all the values of the ListBox.
for i = 0 to ListBox1.ListCount-1
MsgBox(ListBox1.Column(0, i))
next i
answered Jan 25, 2015 at 15:36
timbmgtimbmg
3,1806 gold badges33 silver badges52 bronze badges
Sub get_all_list_item()
For i=0 to Listbox1.ListCount-1
Debug.Print Listbox1.List(0)
Next i
End Sub
answered Jan 29, 2015 at 17:56
Abu NayeemAbu Nayeem
971 gold badge1 silver badge8 bronze badges
Kevin Tang said:
Hello,
I have a ListBox in my VBA Form, named ListBox1
In UserForm_Initialize(), I have two statements to enable List Column Head:
ListBox1.ColumnCount = 2
ListBox1.ColumnHeads = TrueMy questions are:
1) how to set the Column Head’s name? such as «ID», «Name»….
You can’t. There is a bug in VBA and Microsoft never got round to fixing it.
Instead, set ColumnHeads to False, and position a couple of labels
immediately above the listbox whose captions will show the column headings.
2) how to add item into ListBox1 in both columns?? (e.g. Add (00001,
Kevin) )
Two possible approaches to this.
1. If you want to load the entire listbox in one go, put all your items into
a 2-dimensional array, and then assign the array to the List property of the
Listbox.
2. If you just want to add a single new row, use AddItem to add the new row
and assign text to the cell pointed to by the BoundCOlumn property (column 0
by default), Then add the text to the second column by using
ListBox1.List(n, 1) = «My new text», where n is the row number you want.

Доброго времени суток!
Пересмотрел все похожие темы, но ответа на свой вопрос не нашел, помогите пожалуйста, если кто знает.
Заполняю ListBox на форме в Excel через RowSource, открывая для этого нужную рабочую книгу
| Visual Basic | ||
|
ListBox заполняется данными, которые находятся по указанному адресу, проблем нет.
Теперь то же самое хочу проделать в форме приложения Word, используя тот же самый код, но ни один, ни другой вариант не работает, возникает ошибка «RunTime Error 438: object doesn’t support this property or method».
Подозреваю, что в ссылке на адрес диапазона ячеек не хватает объекта «приложение Excel», пробовал применить
| Visual Basic | ||
|
но тоже ничего не получилось. ListBox, расположенный на форме приложения Word данными из рабочей книги Excel не заполняются. Можно ли такое в принципе осуществить и если можно, то как?
Forum Rules |
|



items get added during initialization)

