Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

partial search

0

Hi 

Have a excel table with one column. the coding i have only allows exact match. for eg my column has first names and surnames. if i type, for eg , Robert Mortimer and then search i get a result. However if i only put in robert, i dont get a result. How can i do a partial search and only write in rob or robert but still get a result. 

the VBA coding i am using is as follows

Sub Searchable_List_Simple()

Dim MainSheet As Worksheet
Dim MyTable As ListObject
Set MainSheet = Sheets("Main")
Set MyTable = MainSheet.ListObjects("MyTable")
UserInput = MainSheet.Range("B3").Value
FieldName = MainSheet.Range("D3").Value
If UserInput <> "" Then
    MyTable.AutoFilter.ShowAllData
    If FieldName = "APT" Then
    MyTable.Range.AutoFilter Field:=1, Criteria1:=UserInput
          End If
    End If

End Sub

Thank you for assistance

Anthony

Answer
Discuss

Answers

0
Selected Answer

Hi Anthony and welcome to the Forum.

To filter with a partial string, just add the * wildcard either side of your UserInput, i.e. change this line to add the bits in bold:

MyTable.Range.AutoFilter Field:=1, Criteria1:="*" & UserInput & "*"

That way, a UserInput of "ort" becomes "*ort*" for the purposes of filtering your table and you'd see the rows containing Mortimer, Short and Orthan (i.e. it's case-insensitive).

Hope this fixes your problem.

Discuss

Discussion

p.s. a better title for your question would be Partial filter (since many Forum user are interested in tweaking Don's tutorial Fast Search Entire Excel Workbook & Return All Results and the others like that.
John_Ru (rep: 6142) Jun 6, '22 at 3:09 am
Thank you John
this is exactly what i hoped for
Anthony
mycobblermends (rep: 6) Jun 6, '22 at 3:47 am
That's good. Thanks for selecting my answer Anthony. 
John_Ru (rep: 6142) Jun 6, '22 at 3:55 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login