+ Reply to Thread
Results 1 to 4 of 4

how do I return a HIGHLIGHTED (not selected) item from a ListBox?

Hybrid View

  1. #1
    Registered User
    Join Date
    06-12-2012
    Location
    Dallas, Texas
    MS-Off Ver
    Excel 2010
    Posts
    54

    how do I return a HIGHLIGHTED (not selected) item from a ListBox?

    Friends,

    I have a list box that is populated by the user from a combobox of options. Then a commandbutton is clicked and the listbox is supposed to act as a filter and hide all rows that do not contain the listbox items. I have code that sill highlight all of the code, but I can't return the highlighted item to exucute the rest of my code... What do I do?

    Private Sub CommandButton1_Click()
    
    Dim r As Long
    
    Range("A6").Select
    
    
    r = Range("A6", Range("A6").End(xlDown)).Cells.Count
    
    For x = 0 To r
    
        For i = 0 To CSJList.ListCount - 1
            
            CSJList.Selected(i) = True
                    
            If CSJList.Value <> ActiveCell.Offset(x, 0).Value Then
            
                ActiveCell.Offset(r, 0).EntireRow.Hidden = True
                
            Else
            
            End If
            
        Next i
        
    Next x
    
    End Sub
    As I am running through the code line by line it show CSJList.Value = Null

    Thanks in advance,

    Mike

  2. #2
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: how do I return a HIGHLIGHTED (not selected) item from a ListBox?

    Private Sub CommandButton1_Click()
    
    Dim r As Long
    
    Range("A6").Select
    
    r = Range("A6", Range("A6").End(xlDown)).Cells.Count
    
    For x = 0 To r
        For i = 0 To CSJList.ListCount - 1
            'You don't have to select it to iterate the list.
            'CSJList.Selected(i) = True
                    
            If CSJList.List(i).Value <> ActiveCell.Offset(x, 0).Value Then
                ActiveCell.Offset(r, 0).EntireRow.Hidden = True
            End If
        Next i
    Next x
    End Sub
    You can do away with the combobox and only use the listbox. Set the MultiSelect=True, then the user can select multiple items in the listbox.

    Private Sub CommandButton1_Click()
    
    Dim r As Long
    
    Range("A6").Select
    
    r = Range("A6", Range("A6").End(xlDown)).Cells.Count
    
    For x = 0 To r
        For i = 0 To CSJList.ListCount - 1
            If Not CSJList.Selected(i) Then
                If CSJList.List(i).Value <> ActiveCell.Offset(x, 0).Value Then
                    ActiveCell.Offset(r, 0).EntireRow.Hidden = True
                End If
            End If
        Next i
    Next x
    End Sub
    David
    (*) Reputation points appreciated.

  3. #3
    Registered User
    Join Date
    06-12-2012
    Location
    Dallas, Texas
    MS-Off Ver
    Excel 2010
    Posts
    54

    Re: how do I return a HIGHLIGHTED (not selected) item from a ListBox?

    Tinbendr,

    Thanks so much for your reply but I am getting an object error at this line...

    If CSJList.List(i).Value <> ActiveCell.Offset(x, 0).Value Then
    Any idea why? I am sure that CSJList is the correct name of the object

    Thanks in advance,

    -Mike

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,480

    Re: how do I return a HIGHLIGHTED (not selected) item from a ListBox?

    try dropping the .Value property, which does not exist.

    If CSJList.List(i) <> ActiveCell.Offset(x, 0).Value Then
    Cheers
    Andy
    www.andypope.info

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1