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
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