Solved

userform Listbox search  one to multiple search terms

Posted on 2014-04-03
10
265 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

744 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

13 Experts available now in Live!

Get 1:1 Help Now