Excel VBA display the value of first and last index of selected items on list box

sashimee

New Member
Joined
Aug 31, 2022
Messages
4
Office Version
  1. 365
  2. 2021
  3. 2016
Platform
  1. Windows
Hi all, this is my first time to post here! :)

I am helpless now, I cannot display the value of the first and last index of selected items on a list box thru MsgBox. It only displays me boolean values.

Here is the code:

VBA Code:
x = lb2.ListIndex = 0 'get first index of listbox
MsgBox x

I'm thinking of adding .Value but I don't know where to put that code it gives me error. I appreciate your help on this.

EDIT: I just want to add that the items from the list box are dynamic.
 
Last edited by a moderator:

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Is that a multiselect Listbox ?
If so, try something along these lines:
VBA Code:
Sub test()

    Dim i As Long, sMsg As String
    Dim sRetValues() As String
    
    For i = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(i) = True Then
            sMsg = sMsg & ListBox1.List(i) & vbCrLf
        End If
    Next i
    sRetValues = Split(sMsg, vbCrLf)
    If UBound(sRetValues) <> -1 Then
        sMsg = "First Selected Item: [" & sRetValues(0) & "]" & IIf(UBound(sRetValues) > 1, vbCrLf & _
        "Second Selected Item: [" & sRetValues(UBound(sRetValues) - 1) & "]", "")
        MsgBox sMsg
    Else
        MsgBox "No Items Selected."
    End If

End Sub
 
Upvote 0
Solution
Is that a multiselect Listbox ?
If so, try something along these lines:
VBA Code:
Sub test()

    Dim i As Long, sMsg As String
    Dim sRetValues() As String
   
    For i = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(i) = True Then
            sMsg = sMsg & ListBox1.List(i) & vbCrLf
        End If
    Next i
    sRetValues = Split(sMsg, vbCrLf)
    If UBound(sRetValues) <> -1 Then
        sMsg = "First Selected Item: [" & sRetValues(0) & "]" & IIf(UBound(sRetValues) > 1, vbCrLf & _
        "Second Selected Item: [" & sRetValues(UBound(sRetValues) - 1) & "]", "")
        MsgBox sMsg
    Else
        MsgBox "No Items Selected."
    End If

End Sub

yes, it is multiselect listbox. Thank you! It works now. I just have to tweak some codes to align on my project.
 

Attachments

  • 2022-09-01 13_24_18.png
    2022-09-01 13_24_18.png
    25.1 KB · Views: 7
Upvote 0

Forum statistics

Threads
1,224,737
Messages
6,180,670
Members
452,993
Latest member
FDARYABEE

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top