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

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

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