• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

how to use search option in excel using vba..

shivanaresh

New Member
i will have list contents in a single column, if i search a key word or multiple keywords the result should disply can anyone please help me....
 
upload_2017-6-15_20-5-9.png

above is the example where is i am looking for ball and bat, using key word bat..
please help someone as soon as possible
 

Thanks for responding Chihiro..

1) will it works if i search partial word of cell value.
2) i will type my search key word in the input text box ,my search keywords can be more than one keyword like multiple search criteria. then i will hit search button
if its possible please an clear code.
 
i have made coding like this:

Sub searchbox()

Dim myButton As OptionButton
Dim MyVal As Long
Dim ButtonName As String
Dim sht As Worksheet
Dim myField As Long
Dim DataRange As Range
Dim mySearch As Variant

'Load Sheet into A Variable
Set sht = ActiveSheet

'Unfilter Data (if necessary)
On Error Resume Next
sht.ShowAllData
On Error GoTo 0

'Filtered Data Range (include column heading cells)
Set DataRange = sht.Range("A4:A531") 'Cell Range
'Set DataRange = sht.ListObjects("Table1").Range 'Table

'Retrieve User's Search Input
mySearch = sht.Shapes("UserSearch").TextFrame.Characters.Text 'Control Form
'mySearch = sht.OLEObjects("UserSearch").Object.Text 'ActiveX Control
'mySearch = sht.Range("A1").Value 'Cell Input

'Loop Through Option Buttons
For Each myButton In ActiveSheet.OptionButtons
If myButton.Value = 1 Then
ButtonName = myButton.Text
Exit For
End If
Next myButton
'Determine Filter Field
On Error GoTo HeadingNotFound
myField = Application.WorksheetFunction.Match(ButtonName, DataRange.Rows(1), 0)
On Error GoTo 0
'Filter Data
DataRange.AutoFilter _
Field:=myField, _
Criteria1:="=*" & mySearch & "*", _
Operator:=xlAnd
'Clear Search Field
sht.Shapes("UserSearch").TextFrame.Characters.Text = "" 'Control Form
'sht.OLEObjects("UserSearch").Object.Text = "" 'ActiveX Control
'sht.Range("A1").Value = "" 'Cell Input

Exit Sub

'ERROR HANDLERS
HeadingNotFound:
MsgBox "The column heading [" & ButtonName & "] was not found in cells " & DataRange.Rows(1).Address & ". " & _
vbNewLine & "Please check for possible typos.", vbCritical, "Header Name Not Found!"



End Sub
 
Please use Code tag to post VBA code.

upload_2017-6-15_11-5-58.png

As for your question...
1) Yes
2) Not directly no. If you list multiple partial words, you'll need to use SPLIT function to split the search words into individual parts and loop through them.

Upload sample workbook to get better help.
 
hi can anyone help me..above i have given an example (screen shot).which shows my requirement i am unable to upload an excel as i am in office..
 
Back
Top