+ Reply to Thread
Results 1 to 17 of 17

Filter List Box values based on multiple text boxes

Hybrid View

Medex Filter List Box values based... 09-06-2018, 05:02 AM
ImranBhatti Re: Filter List Box values... 09-06-2018, 08:28 AM
Fluff13 Re: Filter List Box values... 09-06-2018, 10:07 AM
bakerman2 Re: Filter List Box values... 09-06-2018, 11:39 AM
Sintek Re: Filter List Box values... 09-06-2018, 04:21 PM
Medex Re: Filter List Box values... 09-07-2018, 02:41 AM
Medex Re: Filter List Box values... 09-07-2018, 03:16 AM
jindon Re: Filter List Box values... 09-07-2018, 03:25 AM
Medex Re: Filter List Box values... 09-07-2018, 05:12 AM
jindon Re: Filter List Box values... 09-07-2018, 06:46 AM
jindon Re: Filter List Box values... 09-07-2018, 06:13 AM
ImranBhatti Re: Filter List Box values... 09-07-2018, 07:24 AM
Medex Re: Filter List Box values... 09-07-2018, 06:59 AM
jindon Re: Filter List Box values... 09-07-2018, 07:08 AM
Medex Re: Filter List Box values... 09-09-2018, 07:23 AM
jindon Re: Filter List Box values... 09-09-2018, 07:32 AM
Medex Re: Filter List Box values... 09-09-2018, 03:17 PM
  1. #1
    Registered User
    Join Date
    06-27-2018
    Location
    Azerbaijan
    MS-Off Ver
    2013
    Posts
    29

    Filter List Box values based on multiple text boxes

    Hello Dear Members,

    I got a situation make me confuse.

    So, I have one sheet named "MainDataBase" which include range of data with multiple columns and rows.

    If you check attached excel file you will see when I try to edit my data , firstly I bring full range of data into list box.

    Afterwards I'd like to filter the same data placed in list box based on multiple text boxes.

    For the first text box you can see my code (I found that code from internet). But actually I don't know how can I modify it for my situation.

    I am saying what exactly I need : for instance if I filter for worker name I want to see all list box column, because it may need to filter further for other text boxes.

    Thanks for all your support. I am so glad being part of this kind of community.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,785

    Re: Filter List Box values based on multiple text boxes

    You are populating only one column. Try this

    Private Sub WorkerNameTxb_Change()
    
    Dim i As Long
    Dim arrList As Variant
    Dim MDB As Worksheet:    Set MDB = Worksheets("MainDataBase")
    
    Me.DBListBox.Clear
    If MDB.Range("A" & MDB.Rows.Count).End(xlUp).Row > 1 And Trim(Me.WorkerNameTxb.Value) <> vbNullString Then
        arrList = MDB.Range("A1:J" & MDB.Range("A" & MDB.Rows.Count).End(xlUp).Row).Value2
        For i = LBound(arrList) To UBound(arrList)
            If InStr(1, arrList(i, 1), Trim(Me.WorkerNameTxb.Value), vbTextCompare) Then
                With Me.DBListBox
                    .AddItem arrList(i, 1)
                    .Column(1, .ListCount - 1) = arrList(i, 2)
                    .Column(2, .ListCount - 1) = arrList(i, 3)
                    .Column(3, .ListCount - 1) = arrList(i, 4)
                    .Column(4, .ListCount - 1) = arrList(i, 5)
                    .Column(5, .ListCount - 1) = arrList(i, 6)
                    .Column(6, .ListCount - 1) = arrList(i, 7)
                    .Column(7, .ListCount - 1) = arrList(i, 8)
                    .Column(8, .ListCount - 1) = arrList(i, 9)
                    .Column(9, .ListCount - 1) = arrList(i, 10)
                End With
            End If
        Next i
    End If
    If Me.DBListBox.ListCount = 1 Then Me.DBListBox.Selected(0) = True
    
    End Sub
    Last edited by ImranBhatti; 09-06-2018 at 08:34 AM.
    Teach me Excel VBA

  3. #3
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,789

    Re: Filter List Box values based on multiple text boxes

    another option
    Dim MTable As Variant
    
    Private Sub UserForm_Initialize()
    Dim LRow As Long
    Dim LCol As Long
    
    
    With Worksheets("MainDataBase")
        LRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        LCol = .Cells(5, .Columns.Count).End(xlToLeft).Column
        MTable = .Range(.Cells(5, 1), .Cells(LRow, LCol))
    End With
    
    With Me.Controls("DBListBox")
        .Clear
        .ColumnHeads = False
        .ColumnCount = UBound(MTable, 2)
        .List = MTable
        .ColumnWidths = "180;155;155;90;90;102;85;80;75;1"
        .TopIndex = 0
        
        
    End With
    End Sub
    
    
    Private Sub WorkerNameTxb_Change()
    
    Dim i As Long, j As Long
    Dim MDB As Worksheet:    Set MDB = Worksheets("MainDataBase")
    
    Me.DBListBox.Clear
    If MDB.Range("A" & MDB.Rows.Count).End(xlUp).Row > 1 And Trim(Me.WorkerNameTxb.Value) <> vbNullString Then
        For i = LBound(MTable) To UBound(MTable)
            If InStr(1, MTable(i, 1), Trim(Me.WorkerNameTxb.Value), vbTextCompare) Then
                With Me.DBListBox
                    .AddItem MTable(i, 1)
                    For j = 1 To 9
                      .Column(j, .ListCount - 1) = MTable(i, j + 1)
                   Next j
                End With
            End If
        Next i
    End If
    If Me.DBListBox.ListCount = 1 Then Me.DBListBox.Selected(0) = True
    
    End Sub
    Vlaue in red must go at the very top of the module, before any code

  4. #4
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,352

    Re: Filter List Box values based on multiple text boxes

    In my opinion the best way to get filtered list in listbox is to use Autofilter or even better AdvancedFilter.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  5. #5
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Filter List Box values based on multiple text boxes

    Something like this perhaps ...
    Private Sub WorkerNameTxb_Afterupdate()
    Dim Arr, lrow As Long
    Application.ScreenUpdating = False
    With Sheets("MainDataBase")
        With .Range("A4:J" & .Cells(Rows.Count, "A").End(xlUp).Row)
            .AutoFilter 1, WorkerNameTxb.Value & "*"
        End With
        lrow = .Cells(Rows.Count, "A").End(xlUp).Row
        Arr = .Range("A5:J" & lrow).SpecialCells(xlCellTypeVisible).Address
        Me.DBListBox.RowSource = Arr
        .AutoFilterMode = False
    End With
    Application.ScreenUpdating = True
    End Sub
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  6. #6
    Registered User
    Join Date
    06-27-2018
    Location
    Azerbaijan
    MS-Off Ver
    2013
    Posts
    29

    Re: Filter List Box values based on multiple text boxes

    Dear, Sintek your code did't work with me ((.

  7. #7
    Registered User
    Join Date
    06-27-2018
    Location
    Azerbaijan
    MS-Off Ver
    2013
    Posts
    29

    Re: Filter List Box values based on multiple text boxes

    Dear, ImranBhatti your code is working well for each text box separately. but when I try to filter two or more text boxes at the same time it doesn't work properly.

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Filter List Box values based on multiple text boxes

    Try change to
    Option Explicit
    
    Private MTable
    
    Private Sub UserForm_Initialize()
        Dim i As Long
        With Worksheets("MainDataBase").Range("a4").CurrentRegion
            MTable = .Offset(1).Resize(.Rows.Count - 1).Value
        End With
        ReDim Preserve MTable(1 To UBound(MTable, 1), 1 To UBound(MTable, 2) + 1)
        For i = 1 To UBound(MTable, 1)
            MTable(i, UBound(MTable, 2)) = i + 4
        Next
        With Me.Controls("DBListBox")
            .ColumnCount = UBound(MTable, 2) - 1
            .List = MTable
            .ColumnWidths = "180;155;155;90;90;102;85;80;75;1"
            .TopIndex = 0
        End With
    End Sub
    
    
    Private Sub WorkerNameTxb_Change()
    
        Dim i As Long
        Dim arrList As Variant
        With Me.DBListBox
            If Me.WorkerNameTxb.Value <> "" Then
                For i = .ListCount - 1 To 0 Step -1
                    If Not UCase$(.List(i, 0)) Like "*" & UCase$(Me.WorkerNameTxb.Value) & "*" Then .RemoveItem i
                Next
            Else
                .List = MTable
            End If
        End With
    End Sub

  9. #9
    Registered User
    Join Date
    06-27-2018
    Location
    Azerbaijan
    MS-Off Ver
    2013
    Posts
    29

    Re: Filter List Box values based on multiple text boxes

    Dear, Jindon. Thanks for your consideration. I used your code and it worked well when I filter text boxes more than one at the same time. But little problem I have. Suppose that I filtered list box by two or more text boxes and then I clear value of one of those text boxes, list box does not show me result according kept text box values. or another example is that, I wrote "Aqil" in the worker name text box and then if I remove last letter "L" list box will not show result relate to "Aqi". How can I solve this problem. Thanks a lot for your support.

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Filter List Box values based on multiple text boxes

    Quote Originally Posted by Medex View Post
    I wrote "Aqil" in the worker name text box and then if I remove last letter "L" list box will not show result relate to "Aqi". How can I solve this problem. Thanks a lot for your support.
    If you want this to happen, it will be much slower.
    Current setting is to Initialize the ListBox when any textbox is chaned to empty, so if you change any one text box to empty, it will filter the by the rest of text boxes if any of them are filled.

    If you don't care about the speed, change
        With EditData
            If TB.Value = "" Then .DBListBox.List = MTable
    to
        With EditData
            .DBListBox.List = MTable
    In class module called MyTB

  11. #11
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Filter List Box values based on multiple text boxes

    Changed textbox names and more.

    Try the attached.
    Attached Files Attached Files

  12. #12
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,785

    Re: Filter List Box values based on multiple text boxes

    Quote Originally Posted by jindon View Post
    Changed textbox names and more.

    Try the attached.
    7 *s from me.

  13. #13
    Registered User
    Join Date
    06-27-2018
    Location
    Azerbaijan
    MS-Off Ver
    2013
    Posts
    29

    Re: Filter List Box values based on multiple text boxes

    Dear, Jindon I am really grateful to you for your all support and appreciate your help. Your code is high level but I understood main principal. I learned much more things.

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Filter List Box values based on multiple text boxes

    You are welcome and thanks for the rep.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  15. #15
    Registered User
    Join Date
    06-27-2018
    Location
    Azerbaijan
    MS-Off Ver
    2013
    Posts
    29

    Re: Filter List Box values based on multiple text boxes

    Dear Jindon, I am really sorry that I ask about this thread again. but it happen problem with me and I can't solve it.

    I just copy and paste your code into my original file and create proper class module. But when I run code I got error which show that problem is about user form activation.

    Please help me with this problem.
    Attached Files Attached Files

  16. #16
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Filter List Box values based on multiple text boxes

    All you have to do is change all TextBox names.
    Text box for;
    Worker Name -> TB1
    Division -> TB2
    Position -> TB3
    .
    .
    Total -> TB10.

  17. #17
    Registered User
    Join Date
    06-27-2018
    Location
    Azerbaijan
    MS-Off Ver
    2013
    Posts
    29

    Re: Filter List Box values based on multiple text boxes

    Thanks a lot!

+ 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. Replacing specific text in multiple text boxes based on cell values
    By ettevi23 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-17-2017, 09:21 PM
  2. Replies: 4
    Last Post: 05-19-2015, 11:53 PM
  3. [SOLVED] How to auto populate multiple Text Boxes from list based on value of a cell
    By MooseAUH in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-04-2014, 03:40 AM
  4. [SOLVED] Populate Values in Multiple List Boxes based on Multiple Criterias
    By s2jrchoi in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 09-09-2013, 11:39 AM
  5. Filter A Pivot Table Multiple Times Based On Values In List In Another Sheet
    By w.m.christensen in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-24-2013, 03:55 AM
  6. [SOLVED] Filter values based on a master list and an input list
    By PY_ in forum Excel General
    Replies: 8
    Last Post: 04-12-2013, 06:36 AM
  7. [SOLVED] VBA to copy multiple Text boxes values to a sheet
    By sagar007 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-21-2013, 03:45 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