Link to home
Start Free TrialLog in
Avatar of Fordraiders
FordraidersFlag for United States of America

asked on

userform Listbox search one to multiple search terms

excel 2010
vba
userform
Listbox
Textbox

I have a textbox33
 i type in search terms, seperated by a Semi Colon ;

example:
SENSOR;120V

The code i currently have takes each term as (strFilter) seperately and loops through 2 column in the listbox to see if that term exits in either column.
If it does not. It deletes the item from the listbox

What I Need:
I need to take one or all the search terms to see if they exist either in the Column1 or Column2 in the listbox.

But here is the problem:
When i search into Column1 if ANY of the terms gets a hit(true) then. do not delete the row Even if ANY of the terms do not hit in Column2.
Then I also need the opposite to occur.
When i search into Column2 if ANY of the terms gets a hit(true) then. do not delete the row Even if ANY of the terms do not hit in Column1.

strFilter = Split(TextBox33.Text, ";")
Application.ScreenUpdating = False

For lngIndex = ListBox32.ListCount - 1 To 0 Step -1
    bFound = True
    
    For lngPart = 0 To UBound(strFilter)
        If UBound(strFilter) = 0 Then
           If InStr(1, UCase(ListBox32.List(lngIndex, 1)), UCase(strFilter(lngPart))) = 0 Or InStr(1, UCase(ListBox32.List(lngIndex, 2)), UCase(strFilter(lngPart))) = 0 Then
               bFound = False
               Exit For
            End If
        Else ' ubound > 1
             ' assuming bfound is still true
                              ' ok not in both descriptions  delete it
                             If InStr(1, UCase(ListBox32.List(lngIndex, 1)), UCase(strFilter(lngPart))) = 0 And InStr(1, UCase(ListBox32.List(lngIndex, 2)), UCase(strFilter(lngPart))) = 0 Then
                                bFound = False
                                Exit For
                             Else
                                            If InStr(1, UCase(ListBox32.List(lngIndex, 1)), UCase(strFilter(lngPart))) = 0 Or InStr(1, UCase(ListBox32.List(lngIndex, 2)), UCase(strFilter(lngPart))) = 0 Then
                                               bFound = False
                                                 Exit For
                                            End If
                             End If
          End If
    Next
    If Not bFound Then
        ListBox32.RemoveItem (lngIndex)
    End If
Next

Open in new window



This code is not working:

Thanks
fordraiders
Avatar of Jacques Geday
Jacques Geday
Flag of Canada image

Let me recap all this in 1 sentence:

If Any of the terms hit true (wether in Column1 or in Column2) DO NOT REMOVE the row

Right ?
gowlfow
Avatar of Fordraiders

ASKER

yes
You said yes does this means we need now to only focus on:
If Any of the terms hit true (wether in Column1 or in Column2) DO NOT REMOVE the row

and forget about the old way of doing ?
gowflow
ASKER CERTIFIED SOLUTION
Avatar of Jacques Geday
Jacques Geday
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
goflow, \and forget about the old way of doing ?

yes..
whatever you come up with.
Well I asked you you said YES then I asked again you did not reply !!!

So what is it now please state in couple of sentences what do you expect from the macro as it is not clear.

gowflow
Thanks for helping.
gowflow,

next j ??  seems not to be correct ?
Dim bFound As Boolean
Dim lngIndex As Long, lngPart As Long, lngCol As Long
Dim strFilter As Variant

strFilter = Split(TextBox33.Text, ";")
Application.ScreenUpdating = False

For lngIndex = ListBox32.ListCount - 1 To 0 Step -1
    bFound = False
   
    For lngPart = 0 To UBound(strFilter)
        For lngCol = 1 To 2
            '---> if item found flag and exit all loops
            If InStr(1, UCase(ListBox32.List(lngIndex, lngCol)), UCase(strFilter(lngPart))) <> 0 Then
                bFound = True
                Exit For
             End If
        Next J  <------------------------------------ error here
        If bFound Then Exit For
    Next lngPart
   
    '---> If Item Not Found delete it from listbox
    If Not bFound Then
        ListBox32.RemoveItem (lngIndex)
    End If
Next lngIndex
yes sorry my mistake I changed it to be meaningful but did not complete the job !!!

replace
Next  J

by
Next lngCol

I don't understand you accepted the question as answer did you finally got what you want ? it is not clear to me.


gowflow
goflow as an additional update:

I had to add this line:
 If strFilter(lngPart) = "" Then Exit For'  <---- fordraiders added this line




For lngIndex = UserForm2.ListBox32.ListCount - 1 To 0 Step -1
    bFound = False
   
    For lngPart = 0 To UBound(strFilter)
        For lngCol = 3 To 4
            '---> if item found flag and exit all loops
          If strFilter(lngPart) = "" Then Exit For'  <---- fordraiders added this line
            If InStr(1, UCase(UserForm2.ListBox32.List(lngIndex, lngCol)), UCase(strFilter(lngPart))) <> 0 Then
                bFound = True
                Exit For
             End If
        Next lngCol
        If bFound Then Exit For
    Next lngPart

    '---> If Item Not Found delete it from listbox
    If Not bFound Then
        UserForm2.ListBox32.RemoveItem (lngIndex)
    End If
Next lngIndex