+ Reply to Thread
Results 1 to 9 of 9

Search within a ListBox as you type in a TextBox

  1. #1
    Registered User
    Join Date
    07-16-2016
    Location
    Belo Horizonte, Brazil
    MS-Off Ver
    2016
    Posts
    22

    Search within a ListBox as you type in a TextBox

    Greetings, this is my first post here, but you've all helped me a lot actually in other threads already.

    I've been having trouble solving the following issue:

    I have a userform containing a listbox and a textbox among other items. I want the text that is typed in the textbox to be searched throughout the items in the listbox. Since the items are deletable, I couldn't work with fixed ranges.

    I went through all the threads I could find but still found no solution. Hope you get to help me out.

    Cheers

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Search within a ListBox as you type in a TextBox

    Hi Boechat

    Welcome to the Forum!!!

    Attached is an example of what you appear to be asking for.

    http://www.excelforum.com/excel-prog...combo-box.html
    Attached Files Attached Files
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Registered User
    Join Date
    01-22-2013
    Location
    Lowestoft, England
    MS-Off Ver
    2016
    Posts
    17

    Re: Search within a ListBox as you type in a TextBox

    Depending on the number of items in your list-box, I would suggest against searching while typing text in the text box. This is because you would need to add a script in the change event of the text-box, meaning it will run every time a character it added or removed. Being that you would have to iterate through all of the items in the list to search for a match, this could be quite annoying for end users.

    Better would be to add the script to either the exit event of the text-box, or attach it to a clickable button / image allowing the user to trigger the search.


    Something like;

    Please Login or Register  to view this content.
    This will loop through the items in the list-box searching for a match in the first column, and if a match is found, it will be highlighted.

    Hope this helps.

  4. #4
    Registered User
    Join Date
    07-16-2016
    Location
    Belo Horizonte, Brazil
    MS-Off Ver
    2016
    Posts
    22

    Re: Search within a ListBox as you type in a TextBox

    Thank you for the quick reply, Jaslake!

    I'm not quite sure whether I was too dull to find the specific code in the sheets you sent me or if I wasn't clear. Sorry, I'm still a rookie at this haha

    However I'm attaching the codes I have for the current form. There are 2 commands to open other windows, a simple deleting command for old data and the listbox filling command. What I would like to have is my TextBox1 real time filtering all items in the listbox and displaying only the ones containing the typed info.

    ___________________________________________________________________________________________________
    Private Sub UserForm_Activate()
    Me.ListBox1.List = Sheets("Contatos").Range("A2:D500").Value
    End Sub
    ___________________________________________________________________________________________________
    Private Sub CommandButton3_Click()
    Contactsframe.Hide
    Mainframe.Show
    End Sub
    ___________________________________________________________________________________________________
    Private Sub CommandButton4_Click()
    Contactsframe.Hide
    Newcontactframe.Show
    End Sub
    ___________________________________________________________________________________________________
    Private Sub CommandButton5_Click()
    ListBox1.RemoveItem (ListBox1.ListIndex)
    Range("Cont").Cells(Me.ListBox1.ListIndex + 1, 1).EntireRow.Delete
    End Sub
    ___________________________________________________________________________________________________

    Where "Contatos" is the sheet containing the info for the listbox and Cont is the range defined by A2:D500.

    Forgive me for the lack of skill and thanks for your support,

    Boëchat

  5. #5
    Registered User
    Join Date
    07-16-2016
    Location
    Belo Horizonte, Brazil
    MS-Off Ver
    2016
    Posts
    22

    Re: Search within a ListBox as you type in a TextBox

    Quote Originally Posted by stimulus View Post
    Depending on the number of items in your list-box, I would suggest against searching while typing text in the text box. This is because you would need to add a script in the change event of the text-box, meaning it will run every time a character it added or removed. Being that you would have to iterate through all of the items in the list to search for a match, this could be quite annoying for end users.

    Better would be to add the script to either the exit event of the text-box, or attach it to a clickable button / image allowing the user to trigger the search.


    Something like;

    Please Login or Register  to view this content.
    This will loop through the items in the list-box searching for a match in the first column, and if a match is found, it will be highlighted.

    Hope this helps.
    Hey, Stimulus, thanks for the reply!

    The list would never be bigger than 100 items, so I think it would be OK to use the search function... The issue is that I've been requested to program it strictly in that manner, so I don't have a lot of space for maneuvers here haha

    Any ideas on how to make it work?

    Thank you!

  6. #6
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Search within a ListBox as you type in a TextBox

    Hi Boechat

    If you're interested I'll look at it.

    Please attach a sample workbook the Structure of which is the same as your actual workbook.

    Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    INCLUDE ANY CODE YOU'RE USING OR HAVE TRIED.

    Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

  7. #7
    Registered User
    Join Date
    07-16-2016
    Location
    Belo Horizonte, Brazil
    MS-Off Ver
    2016
    Posts
    22

    Re: Search within a ListBox as you type in a TextBox

    Hello again,

    thanks for your support John! I managed to achieve what I wanted using this code (just in case someone needs it in future):
    ______________________________________________________________________________________
    Private Sub TextBox1_Change()
    Dim i As Long
    Dim testString As String
    testString = LCase("*" & TextBox1.Text & "*")

    Call FillListBoxWithAll
    With ListBox1
    For i = .ListCount - 1 To 0 Step -1
    If (Not (LCase(.List(i, 0)) Like testString) And (Not (LCase(.List(i, 1)) Like testString))) _
    And (Not (LCase(.List(i, 2)) Like testString) And (Not (LCase(.List(i, 3)) Like testString))) Then
    .RemoveItem i
    End If
    Next i
    End With

    End Sub
    ______________________________________________________________________________________
    Sub FillListBoxWithAll()
    ListBox1.List = Range("A2:D500").Value
    End Sub
    ______________________________________________________________________________________
    Private Sub UserForm_Initialize()
    Call FillListBoxWithAll
    End Sub


    Hope it helps someone sometime!

    Cheers

  8. #8
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Search within a ListBox as you type in a TextBox

    Cross-posted http://www.mrexcel.com/forum/excel-q...r-listbox.html

    Boechat, please read the link in my sig about cross posting. The rules of this forum (and most others) require that if you post your question on other forums, you post a link to the other forum. The link in my signature explains why.

    Thanks.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  9. #9
    Registered User
    Join Date
    07-16-2016
    Location
    Belo Horizonte, Brazil
    MS-Off Ver
    2016
    Posts
    22

    Re: Search within a ListBox as you type in a TextBox

    Notice taken! Sorry for the inconvenience!

    Boëchat

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. excel - search in listbox via textbox change?
    By rdm34 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-17-2015, 09:19 PM
  2. VBA - Adding Search to Listbox based on Textbox value
    By nimesh29 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-09-2014, 06:47 PM
  3. [SOLVED] VBA code to filter listbox as you type in a textbox Solved by : Sixthsense
    By HaroonSid in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-27-2014, 10:51 AM
  4. VBA Code To Filter Listbox As You Type In A Textbox
    By Benjamin2008 in forum Excel General
    Replies: 3
    Last Post: 12-20-2013, 08:57 PM
  5. Remove items from a listbox as you type in a textbox
    By Bishonen in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-03-2013, 04:10 PM
  6. Search listbox from textbox
    By zplugger in forum Excel General
    Replies: 12
    Last Post: 08-23-2012, 08:11 PM
  7. [SOLVED] Textbox to search listbox
    By zplugger in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-22-2012, 08:25 AM

Tags for this Thread

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