?
Solved

userform Listbox search  one to multiple search terms

Posted on 2014-04-03
10
Medium Priority
?
274 Views
Last Modified: 2014-04-29
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
0
Comment
Question by:Fordraiders
  • 5
  • 5
10 Comments
 
LVL 31

Expert Comment

by:gowflow
ID: 39976228
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
0
 
LVL 3

Author Comment

by:Fordraiders
ID: 39976588
yes
0
 
LVL 31

Expert Comment

by:gowflow
ID: 39977333
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 31

Accepted Solution

by:
gowflow earned 2000 total points
ID: 39977361
Here is the code that will ONLY remove the item in the list if it is NOT found in any of the columns.

Sub test()
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
        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
End Sub

Open in new window


gowflow
0
 
LVL 3

Author Comment

by:Fordraiders
ID: 39979136
goflow, \and forget about the old way of doing ?

yes..
whatever you come up with.
0
 
LVL 31

Expert Comment

by:gowflow
ID: 39979720
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
0
 
LVL 3

Author Closing Comment

by:Fordraiders
ID: 39980783
Thanks for helping.
0
 
LVL 3

Author Comment

by:Fordraiders
ID: 39982102
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
0
 
LVL 31

Expert Comment

by:gowflow
ID: 39982455
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
0
 
LVL 3

Author Comment

by:Fordraiders
ID: 40031239
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
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question