welshraz
New Member
- Joined
- Apr 29, 2016
- Messages
- 40
- Office Version
- 365
- Platform
- Windows
Afternoon,
I have the following code which performs a search etc. on a table, with a search text box and button on the same page. What I would like is to have a search take place on a different tab so that the user cannot see the data in full. Is this something that is possible?
I have the following code which performs a search etc. on a table, with a search text box and button on the same page. What I would like is to have a search take place on a different tab so that the user cannot see the data in full. Is this something that is possible?
VBA Code:
Sub SearchBox()
'PURPOSE: Filter Data on User-Determined Column & Text/Numerical value
Dim myButton As OptionButton
Dim SearchString As String
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.ListObjects("Contacts1").Range 'Table
'Retrieve User's Search Input
mySearch = sht.OLEObjects("UserSearch").Object.Text 'ActiveX Control
'Determine if user is searching for number or text
If IsNumeric(mySearch) = True Then
SearchString = "=" & mySearch
Else
SearchString = "=*" & mySearch & "*"
End If
'Loop Through Option Buttons
For Each myButton In sht.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:=SearchString, _
Operator:=xlAnd
'Clear Search Field
sht.OLEObjects("UserSearch").Object.Text = "" 'ActiveX Control
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
Last edited by a moderator: