Search certain word to filter a table using vba.

zulzzz88

New Member
Joined
Mar 29, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi, I want to create a search box to make it easier for my colleague to find information from 1 table. However, from my current vba module, they need to know the exact starting word to find what they are looking for. How can I set it so that they can only write certain words in the name? For example, they can just key in “motor”, in other to look for list “air bearing motor”.

VBA Code:
Sub Searchable_List()

Dim Mainsheet As Worksheet
Dim Inventory As ListObject

Set Mainsheet = Sheets("Search")
Set Inventory = Mainsheet.ListObjects("Inventory")

UserInput = Mainsheet.Range("C3") & "*"
Fieldname = Mainsheet.Range("C5").Value

'Check if there is any user input.
If UserInput <> "" Then
    'User input a value,so,proceed
    
    'Clear any filters.
    Inventory.AutoFilter.ShowAllData
    
    'Apply new filters.
    If Fieldname = "Part Number" Then
        'Filter by Part Number
        
        Inventory.Range.AutoFilter Field:=1, Criteria1:=UserInput
        
    ElseIf Fieldname = "Part Name" Then
        'Filter by Part Name
        
        Inventory.Range.AutoFilter Field:=2, Criteria1:=UserInput
        
    ElseIf Fieldname = "Material Code" Then
        'Filter by Material Code
        
        Inventory.Range.AutoFilter Field:=3, Criteria1:=UserInput
        
    End If

End If

      
End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi & welcome to MrExcel.
How about
VBA Code:
UserInput = "*" & Mainsheet.Range("C3") & "*"
 
Upvote 0
Hi & welcome to MrExcel.
How about
VBA Code:
UserInput = "*" & Mainsheet.Range("C3") & "*"
Hai, tq for reply. Its work for me now.

However, I have 1 more question to ask. Why I only can search for item that have text format only? When i try to filter that only have numbering, it shows nothing?

For example,

List Item
1. 1N2022
2. 191101
3. 20NN23

From above list, only item no 1 and no 3 will show up, but when i search item no 2, it shows nothing. Is that because there is no value setting in the coding?

Tq.
 
Upvote 0
You can only use wild card matching with text.
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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