Lookup script needed

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
HelloWorld,

I have being struggling with this for a while now.

Say I have this data in my database :

01-02-03-04-05

I can use a textbox input to lookup say 03-04

That is when the numbers follow each other.

Now I am trying to get a script that can take the input of say 02-05 from my textbox and still find this
01-02-03-04-05 from the database.

That is I am looking for a script that can take the textbox input and find if those numbers are found in an entry.

Thanks in advance

Below is the code I have now:
Code:
[/COLOR]Sub Lookup()
    Me.lstView.ColumnCount = 6
    Dim myArray As Variant
    myArray = [c7].Resize(, Me.lstView.ColumnCount + 1).Value
    Me.lstView.List = myArray
    Me.lstView.Clear
    term = Me.CmbClass.Value
    With Sheets(term).Range("C7:C1007")
    Set rngFind = .Find(what:=Me.Rw2.Text, After:=Sheets(term).[C1007], LookIn:=xlValues, lookat:=xlWhole, SearchDirection:=xlNext)
    If Not rngFind Is Nothing Then
        strFirstFind = rngFind.Address
    Do
    If rngFind.Row > 1 Then
        Me.lstView.AddItem rngFind.Offset(0, -1).Text
    For i = 1 To 5
        Me.lstView.List(Me.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
    End If
    End With
End Sub
[COLOR=#333333]
 
Last edited:

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Hi kelly mort,

I am stumped with your request and I can’t “see” what you are trying to accomplish. When you say

That is when the numbers follow each other.

Why 01-02, or 04-05 don’t count? These numbers follow each other per your example?!

If you are looking to capture input from the user and then compare that substring to see if it occures in your dataset, I would suggest changing the line

Code:
Set rngFind = .Find(what:=Me.Rw2.Text, After:=Sheets(term).[C1007], LookIn:=xlValues, lookat:=xlWhole, SearchDirection:=xlNext)


to read like:

Code:
Set rngFind = .Find(what:=Me.Rw2.Text, After:=Sheets(term).[C1007], LookIn:=xlValues, lookat:=xlPart, SearchDirection:=xlNext)[COLOR=#333333]
[/COLOR]

Also, if you don’t mind. There are some standards that are in use such as declaring all your variables at the very top of the subroutine. This is useful for the humans and not the machine, it makes your code more “readable” by others. Sticking to these standards is a good idea.
 
Last edited:
Upvote 0
Hi kelly mort,

I am stumped with your request and I can’t “see” what you are trying to accomplish. When you say



Why 01-02, or 04-05 don’t count? These numbers follow each other per your example?!

If you are looking to capture input from the user and then compare that substring to see if it occures in your dataset, I would suggest changing the line

Code:
Set rngFind = .Find(what:=Me.Rw2.Text, After:=Sheets(term).[C1007], LookIn:=xlValues, lookat:=xlWhole, SearchDirection:=xlNext)


to read like:

Code:
Set rngFind = .Find(what:=Me.Rw2.Text, After:=Sheets(term).[C1007], LookIn:=xlValues, lookat:=xlPart, SearchDirection:=xlNext)[COLOR=#333333]
[/COLOR]

Also, if you don’t mind. There are some standards that are in use such as declaring all your variables at the very top of the subroutine. This is useful for the humans and not the machine, it makes your code more “readable” by others. Sticking to these standards is a good idea.

Yeah,

I am aware of the xlPart.

But this can only capture and compare when the data is continuous like

If there is a data in the system as 00-90-54-65-11 and I enter say 90-65 in the textbox, it will return nothing. Which should not be the case. I will like the code to pull out the above data since the 90 and 65 are present .
 
Upvote 0

Forum statistics

Threads
1,215,262
Messages
6,123,939
Members
449,134
Latest member
NickWBA

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