Excel VBA UserForm Listbox Get Selected Items

preview_player
Показать описание
Excel VBA UserForm Listbox Learn how to get selected items.

The source code used in this video:

Private Sub cbOK_Click()
Dim SelectedItems As String
For i = 0 To lbDays.ListCount - 1
If lbDays.Selected(i) = True Then
SelectedItems = SelectedItems & lbDays.List(i) & vbNewLine
End If
Next i

If SelectedItems = "" Then
MsgBox "Nothing selected"
Else
MsgBox "Selected Items: " & vbNewLine & SelectedItems
End If

End Sub

Private Sub UserForm_Initialize()

With lbDays
.AddItem "Monday"
.AddItem "Tuesday"
.AddItem "Wednesday"
End With

End Sub
Рекомендации по теме
Комментарии
Автор

This is very helpful, but please indent the code so show levels!

CQXRay
Автор

Very helpfull, thank you for sharing knowledge with us.

As I am new to VBA but I am creating a userform for incoming and outgoing documents.
If I want to do the opposite that you did in the video is it possible? I want to get the data that was paste in a cell shown in the listbox. It should what was selected, is this possible.
Searching on the internet but can't find any thing. Hope to get a reply. Thanks in advance for helping.

suraksha
Автор

Hi.
Great video. I have one question tough. I'm creating a document in which I would like to include the selected items in cells underneath each other. Is this possible?

markbondorphfloor
Автор

Hi, how to select from the listbox without the blue highlight?

rhaa
Автор

how to cant select repeat item on listbox ? or how to when select that item item will disappear ? thank you

jirayutplodprong