Solved

userform Listbox search  one to multiple search terms

Posted on 2014-04-03
10
266 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 29

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 29

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
 
LVL 29

Accepted Solution

by:
gowflow earned 500 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 29

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 29

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

861 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now