Consulting

Results 1 to 5 of 5

Thread: Solved: Listbox Selected Items Count?

  1. #1

    Solved: Listbox Selected Items Count?

    Hi.
    Usually i use codes like this:
    [vba]
    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
    [/vba]

    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

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    [vba]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[/vba]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    Thanks Lucas.
    Probably my quession was so abstruse.
    I was thinking solusion like function.[vba]Sub USC()
    Dim UsedCells As Long, x As Long
    UsedCells = Application.WorksheetFunction.CountA(Range("A:A"))
    x = UsedCells
    MsgBox x
    End Sub
    [/vba]

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

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    [vba]
    For i = 0 To (1 less than number of items)
    If ListBox1.Selected(i) = True Then
    NumSelections = NumSelections + 1
    End If
    Next i

    [/vba]

  5. #5
    Hi.
    Probably function like: ...Appl.UFormFunction.CountSelectedItems(ListBox1) impossible.

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •