Code Sub SearchBox() to search multiple columns

jzimm009

New Member
Joined
Oct 23, 2017
Messages
1
I'm running into an issue with Sub Searchbox()

I have spreadsheet with 3 columns (First Name, Last Name, and Phone#) with 25 contacts. I set up a text box and a form control SearchBox button linked to the macro that wiill search the list to match names entered in textbox and autofilter the contact (row).

The problem I'm running into is figuring out how to search FIRST OR LAST NAME. I have to manually adjust the 'filter data (red text below) in order to search the different columns. Is there a way to search either?

thanks!

Sub SearchBox()


Dim MyVal As Long
Dim sht As Worksheet
Dim myField As Variant
Dim myField1 As Variant
Dim DataRange As Range
Dim mySearch As Variant

'Load Sheet into A Variable
Set sht = ActiveSheet


'Unfilter Data
On Error Resume Next
sht.ShowAllData
On Error GoTo 0

'Filtered Data Range
Set DataRange = sht.Range("A2:C28")


'Retrieve User's Search Input
mySearch = sht.Shapes("UserSearch").TextFrame.Characters.Text



' On Error GoTo HeadingNotFound
' myField = Application.WorksheetFunction.Match(DataRange.Rows(1), 0)
'On Error GoTo 0


'Determine Filter Field
myField = 1
myField1 = 1



'Filter Data
DataRange.AutoFilter _
Field:=myField, _
Criteria1:="=*" & mySearch & "*", _
Operator:=xlAnd

DataRange.AutoFilter _
Field:=myField1, _
Criteria1:="=*" & mySearch & "*", _
Operator:=xlAnd


'Clear Search Field
sht.Shapes("UserSearch").TextFrame.Characters.Text = ""


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
 

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,223,909
Messages
6,175,314
Members
452,634
Latest member
cpostell

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