PDA

View Full Version : Solved: Listbox Selected Items Count?



omnibuster
03-25-2010, 01:52 PM
Hi.
Usually i use codes like this:

Private Sub Cmd2_Click()
Dim item As Long, I As Long, msg As String
With ListBox1
For I = 0 To .ListCount - 1
If .Selected(I) Then
msg = msg & .List(I) & vbNewLine
End If
Next I
End With
If msg = vbNullString Then
MsgBox "Nothing was selected! Please make a selection!"
Exit Sub
Else

End If
End Sub


But in case if i need only value= selected.items?
How (is it possibe) put work function like this:
varSelectedItems = Listbox1.Selected.Items.Count
MsgBox varSelectedItems

lucas
03-25-2010, 02:58 PM
Option Explicit
Private Sub ListBox1_Change()
Dim intIndex As Integer
Dim intCount As Integer

With ListBox1
For intIndex = 0 To .ListCount - 1
If .Selected(intIndex) Then intCount = intCount + 1
Next
End With
Label1.Caption = "Selected " & intCount & " out of " & ListBox1.ListCount
End Sub
Private Sub UserForm_Initialize()
With ListBox1
.AddItem "A"
.AddItem "B"
.AddItem "C"
.AddItem "D"
.AddItem "E"
.AddItem "F"
.AddItem "G"
End With
Label1.Caption = "None Selected"
End Sub

omnibuster
03-25-2010, 03:34 PM
Thanks Lucas.
Probably my quession was so abstruse.
I was thinking solusion like function.Sub USC()
Dim UsedCells As Long, x As Long
UsedCells = Application.WorksheetFunction.CountA(Range("A:A"))
x = UsedCells
MsgBox x
End Sub


In my case function like:
SelectedItems = Appl.UFormFunction.CountSelectedItems(ListBox1)

SamT
03-25-2010, 04:02 PM
For i = 0 To (1 less than number of items)
If ListBox1.Selected(i) = True Then
NumSelections = NumSelections + 1
End If
Next i

omnibuster
03-29-2010, 12:20 PM
Hi.
Probably function like: ...Appl.UFormFunction.CountSelectedItems(ListBox1) impossible.

I solved this way.
Maybe not all correct but works.