?
Solved

userform Listbox search  one to multiple search terms

Posted on 2014-04-03
10
Medium Priority
?
271 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

762 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