Results 1 to 6 of 6

Thread: [RESOLVED] Excel 2003 - Find cell value match in sheet Listbox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    222

    Resolved [RESOLVED] Excel 2003 - Find cell value match in sheet Listbox

    First off, the code below is working for finding and selecting the value in a Listbox that matches the value in Cell A1. However I pieced this together through trial and error so I am not sure whether or not I did it in an unorthodox or sloppy manner.

    So my first question is my code ok?

    Second question is how difficult would it be to sort the items in the Listbox?
    -- if it requires API's at all then I'd prefer to skip it, but I was thinking it probably can be done in a straight forward manner.

    I've attached a sample workbook

    Thanks

    Frank

    Edit: I do realize that I should encase most of it between With and end with, I'll do that later

    Code:
    Option Explicit
    
    Private Sub CommandButton1_Click()
          Dim i As Integer
          
          For i = 1 To ListBox1.ListCount
              
              ListBox1.Selected(i - 1) = True
              
              If UCase(ListBox1.Value) = UCase(Range("A1").Value) Then
                  Exit For
              End If
    
          Next i
    End Sub
    
    Private Sub CommandButton2_Click()
    
    With Me.ListBox1
        .Clear
        .AddItem "pear"
        .AddItem "orange"
        .AddItem "three"
        .AddItem "GRAPE"
        .AddItem "Peach"
    End With
    
    End Sub
    Attached Files Attached Files
    Last edited by SQLADOman; Jan 21st, 2011 at 04:22 PM.

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Excel 2003 - Find cell value match in sheet Listbox

    Yes the code is Ok.

    To sort the items in a listbox, use this

    First of all create a sheet and name it "Temp" and then hide that sheet. Once that is done replace all your code above with this...

    Code:
    Option Explicit
    
    Dim I As Long, J As Long
    Dim lastRow As Long
    
    Private Sub CommandButton1_Click()
        For I = 1 To ListBox1.ListCount
            ListBox1.Selected(I - 1) = True
            If UCase(ListBox1.Value) = UCase(Range("A1").Value) Then
                Exit For
            End If
        Next I
    End Sub
    
    Private Sub CommandButton2_Click()
        With Me.ListBox1
            .Clear
            .AddItem "pear"
            .AddItem "orange"
            .AddItem "Three"
            .AddItem "GRAPE"
            .AddItem "Peach"
        End With
        
        For I = 0 To ListBox1.ListCount - 1
            Sheets("Temp").Range("A" & I + 1) = ListBox1.List(I)
        Next
        
        lastRow = Sheets("Temp").Range("A" & Rows.Count).End(xlUp).Row
        
        Sheets("Temp").Range("A1:A" & lastRow).Sort Key1:=Sheets("Temp").Range("A1"), Order1:=xlAscending, _
        Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
        
        ListBox1.Clear
        
        For I = 1 To lastRow
            ListBox1.AddItem Sheets("Temp").Range("A" & I)
        Next
        
        Sheets("Temp").Columns("A:A").ClearContents
    End Sub
    Hope this helps...

    Sid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel 2003 - Find cell value match in sheet Listbox

    you can use any sorting alogrithym to sort an array, from the contents of listbox, but different contents may need different handling
    numeric values are not good when sorted as strings, like 22 will come before 7 etc
    sorting values in a sheet can work fine, but possibly comes under the heading of unorthodox, not to provide your own code
    as listboxes contain limited number of items, in most cases a bubblesort would be quite adequate, but for larger number of items quick sort, or other more advanced sorting would be advantageous

    you do not need this line in your code and it has no adavantage
    ListBox1.Selected(i - 1) = True
    or if you want to finish with the item selected, move to immediately before exit for, so it is only called the one time
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    222

    Re: Excel 2003 - Find cell value match in sheet Listbox

    Thanks Koolsid for the code and thanks Pete for the tips

    Now that I went to use real data in the Listbox I found that some times the Customer name or a portion of it is also contained within the address of a different customer, so depending on what order it is within the list the selection is some times incorrect

    In other words I need to search the customer name and select it only if a first word match is found in the Listbox.

    and if you could show me how to dump the list items into an array and sort it I would enjoy learning how to do that. I do already know how to reload the array back into the Listbox.

    Thanks

    Edit:#2
    Customer List
    Harvey
    Reach
    ati
    sac

    BENCO
    Code:
    Private Sub CommandButton2_Click()
    
    With Me.ListBox1
        .Clear
        .AddItem "Harvey Inc 1703 Sacramento Way BUENA PARK,CA. 90599"
        .AddItem "Benco 1222 Reach Circle Anaheim CA 92586"
        .AddItem "REACH INTERNATIONAL 6900 ORANGETHORPE AVE San Diego,CA. 90620"
        .AddItem "SAC COMPANY 122 Bencotton Dr Fulerton,CA. 90591"
        .AddItem "ATI LTD Inc 1703 Harvey Street BUENA PARK,CA. 90599"
    End With
    
    End Sub
    Attached Files Attached Files
    Last edited by SQLADOman; Jan 22nd, 2011 at 12:10 AM.

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel 2003 - Find cell value match in sheet Listbox

    try like
    vb Code:
    1. If UCase(Left(ListBox1.Value, InStr(ListBox1.Value, " ") - 1)) = UCase(ActiveCell.Value) Then
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    222

    Re: Excel 2003 - Find cell value match in sheet Listbox

    Thanks Pete.

    Works like a charm

Posting Permissions

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



Click Here to Expand Forum to Full Width