Closed Thread
Results 1 to 4 of 4

listbox highlight selection

  1. #1
    Bobby
    Guest

    listbox highlight selection

    Hi, after sorting information that I show in a listbox I would like to
    highlight the first item showing up at top of the listbox(right now I
    get by default a dotted line under the first item)
    Thank's ahead for any hint!
    My code:
    Dim NoDupes As New Collection
    Dim i As Integer, j As Integer
    Dim Swap1, Swap2, Item
    Sub slist()
    i = 1
    Set NoDupes = New Collection
    For Each ws In Worksheets
    NoDupes.Add (Worksheets(i).Name)
    i = i + 1
    Next ws

    ' Sort the collection
    For i = 1 To NoDupes.Count - 1
    For j = i + 1 To NoDupes.Count
    If NoDupes(i) > NoDupes(j) Then
    Swap1 = NoDupes(i)
    Swap2 = NoDupes(j)
    NoDupes.Add Swap1, before:=j
    NoDupes.Add Swap2, before:=i
    NoDupes.Remove i + 1
    NoDupes.Remove j + 1
    End If
    Next j
    Next i

    ' Add the sorted items to a ListBox
    Userform1.ListBox1.Clear
    For Each Item In NoDupes
    Userform1.ListBox1.AddItem Item
    Next Item

    Userform1.Show
    Set NoDupes = New Collection
    End Sub


  2. #2
    Dave Peterson
    Guest

    Re: listbox highlight selection

    This worked ok for me. (And I put all the work into the userform_initialize
    event:

    Option Explicit
    Sub userform_initialize()

    Dim NoDupes As New Collection
    Dim i As Integer, j As Integer
    Dim Swap1, Swap2, Item
    Dim ws As Worksheet

    Set NoDupes = New Collection

    For Each ws In Worksheets
    NoDupes.Add ws.Name
    Next ws

    ' Sort the collection
    For i = 1 To NoDupes.Count - 1
    For j = i + 1 To NoDupes.Count
    If NoDupes(i) > NoDupes(j) Then
    Swap1 = NoDupes(i)
    Swap2 = NoDupes(j)
    NoDupes.Add Swap1, before:=j
    NoDupes.Add Swap2, before:=i
    NoDupes.Remove i + 1
    NoDupes.Remove j + 1
    End If
    Next j
    Next i

    ' Add the sorted items to a ListBox
    Me.ListBox1.Clear
    For Each Item In NoDupes
    Me.ListBox1.AddItem Item
    Next Item

    Me.ListBox1.ListIndex = 0

    Set NoDupes = Nothing
    End Sub
    Private Sub CommandButton1_Click()
    Unload Me
    End Sub

    Bobby wrote:
    >
    > Hi, after sorting information that I show in a listbox I would like to
    > highlight the first item showing up at top of the listbox(right now I
    > get by default a dotted line under the first item)
    > Thank's ahead for any hint!
    > My code:
    > Dim NoDupes As New Collection
    > Dim i As Integer, j As Integer
    > Dim Swap1, Swap2, Item
    > Sub slist()
    > i = 1
    > Set NoDupes = New Collection
    > For Each ws In Worksheets
    > NoDupes.Add (Worksheets(i).Name)
    > i = i + 1
    > Next ws
    >
    > ' Sort the collection
    > For i = 1 To NoDupes.Count - 1
    > For j = i + 1 To NoDupes.Count
    > If NoDupes(i) > NoDupes(j) Then
    > Swap1 = NoDupes(i)
    > Swap2 = NoDupes(j)
    > NoDupes.Add Swap1, before:=j
    > NoDupes.Add Swap2, before:=i
    > NoDupes.Remove i + 1
    > NoDupes.Remove j + 1
    > End If
    > Next j
    > Next i
    >
    > ' Add the sorted items to a ListBox
    > Userform1.ListBox1.Clear
    > For Each Item In NoDupes
    > Userform1.ListBox1.AddItem Item
    > Next Item
    >
    > Userform1.Show
    > Set NoDupes = New Collection
    > End Sub


    --

    Dave Peterson

  3. #3
    Bobby
    Guest

    Re: listbox highlight selection

    Thank's Dave! Tell me how would you call your Sub userform_initialize()
    from a:
    Private Sub Workbook_Open()
    Thank's!


  4. #4
    Dave Peterson
    Guest

    Re: listbox highlight selection

    I would use the workbook_open event to show the form.

    sub workbook_open()
    userform1.show
    end sub



    Bobby wrote:
    >
    > Thank's Dave! Tell me how would you call your Sub userform_initialize()
    > from a:
    > Private Sub Workbook_Open()
    > Thank's!


    --

    Dave Peterson

Closed 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