VBA lookup challenge

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Hello every one,
I have yet another challenge here:
Below is the code I am using to search for data and populate my listbox with. The input is taken from the textbox called txtSearch. But now I have another box(Combobox) called txtOption, and this has the letters A B C D or can even be blank. I am thinking of modifying the code below so that it can filter first from the txtOption then the txtSearch. So when I enter a keyword to search from the txtSearch, it will look at the keyword and display only names assigned to the option in txtOption in the listbox. I am doing the thinking but the image is not clear yet and I need someone here to help pull me out. Thanks
Kelly
Code:
Sub Lookup()
    On Error GoTo errHandler:
        lstView.ColumnCount = 23
            Dim myArray As Variant
                myArray = [C7].Resize(, lstView.ColumnCount + 1).Value
                    lstView.List = myArray
                        lstView.Clear
                    sht = CmbClass.Value
                With Sheets(sht).Range("C7:C110")
            Set rngFind = .Find(What:=txtSearch.Text, After:=Sheets(sht).[C110], LookIn:=xlValues, lookat:=xlPart, _
        SearchDirection:=xlNext)
    If Not rngFind Is Nothing Then
strFirstFind = rngFind.Address
Do
    If rngFind.Row > 1 Then
            lstView.AddItem rngFind.Offset(0, -1).Text
                lstView.List(lstView.ListCount - 1, 1) = rngFind.Text
                    For i = 2 To 22
                        lstView.List(lstView.ListCount - 1, i) = rngFind.Offset(0, i - 1).Text
                            Next i
                                End If
                                    Set rngFind = .FindNext(rngFind)
                                        Loop While Not rngFind Is Nothing And rngFind.Address <> strFirstFind
                                    Else: 'Reset1
                End If
            End With
        On Error GoTo 0
    Exit Sub
errHandler::
    MsgBox "Check your entry for errors", vbCritical, "Error Alert!"
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Forum statistics

Threads
1,214,814
Messages
6,121,711
Members
449,049
Latest member
THMarana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top