How to reference list box values/text
How to reference list box values/text
Thanks Thanks:  0
Likes Likes:  0
Results 1 to 4 of 4

Thread: How to reference list box values/text

  1. #1
    Guest

    Default

     
    How do you reference the text or values in a List Box. i.e. suppose I want to unhide a sheet if a box is checked or highlighted in the list box, how do I reference the value and what function do I use (i.e. ListBox_Change, ListBox_Click, etc...).

    For a combo box I would write

    Private Sub ComboBox_Change()
    If ComboBox = "Argentina" Then
    Sheets("Argentina").Visible = True
    End If
    End Sub

    How do I do this same thing with a list box. I want to be able to select multiple countries and unhide the appropriate sheets. Thanks

  2. #2
    Guest

    Default

    Each value/text in a listbox is automatically assigned a number

    The first value is 0, second 1, etc.

    Use this for what you are trying to do.

    Private Sub ListBox_Change()
    If SolutionBox.Selected(0) = True Then
    Sheets("Argentina").Visible = True
    Else
    Sheets("Argentina").Visible = False
    End If
    End Sub

  3. #3
    Guest

    Default

    On 2002-03-08 06:49, Anonymous wrote:
    How do you reference the text or values in a List Box. i.e. suppose I want to unhide a sheet if a box is checked or highlighted in the list box, how do I reference the value and what function do I use (i.e. ListBox_Change, ListBox_Click, etc...).

    For a combo box I would write

    Private Sub ComboBox_Change()
    If ComboBox = "Argentina" Then
    Sheets("Argentina").Visible = True
    End If
    End Sub

    How do I do this same thing with a list box. I want to be able to select multiple countries and unhide the appropriate sheets. Thanks
    Something like this should do what you want:

    Code:
    Private Sub CommandButton1_Click()
        Dim intI As Integer
        
        For intI = 0 To ListBox1.ListCount - 1
            If ListBox1.Selected(intI) Then
                Sheets(ListBox1.List(intI)).Visible = True
            End If
        Next intI
    End Sub
    What I did was fill my ListBox with the names of the sheets in my workbook. Then I added a command button that, when clicked, unhides the sheets that are selected in the ListBox. Like the other Anon poster said, the ListBox items are zero-based. In other words, they start at zero instead of one. This is why you need to use ListBox1.ListCount - 1...if there are 4 sheet names in your list box, we need to go from 0 to 3 instead of from 1 to 4 (but the count tells us there are 4 items in your list).

    I hope this helps,

    Russell

  4. #4
    MrExcel MVP Russell Hauf's Avatar
    Join Date
    Feb 2002
    Location
    Portland, OR Area - USA
    Posts
    1,605
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

      
    Sorry, the last message was from me.

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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

 

 
DMCA.com