hi guys,
i need to search and filter time . it is searching text and numerical values. but when i search for time it is hiding the data.
below is my database and the code i used.
[TABLE="width: 201"]
<colgroup><col style="text-align: center;"><col style="text-align: center;"></colgroup><tbody>[TR]
[TD="align: center"]LINK[/TD]
[TD="align: center"]CLINK[/TD]
[/TR]
[TR]
[TD="align: center"]8:00:00[/TD]
[TD="align: center"]er[/TD]
[/TR]
[TR]
[TD="align: center"]GHJ[/TD]
[TD="align: center"]45[/TD]
[/TR]
[TR]
[TD="align: center"]SALIM[/TD]
[TD="align: center"]123[/TD]
[/TR]
[TR]
[TD="align: center"]SAIF[/TD]
[TD="align: center"]DF[/TD]
[/TR]
[TR]
[TD="align: center"]9:20:00[/TD]
[TD="align: center"]657[/TD]
[/TR]
[TR]
[TD="align: center"]10:20:00[/TD]
[TD="align: center"]78989[/TD]
[/TR]
[TR]
[TD="align: center"]10:35:00[/TD]
[TD="align: center"]900[/TD]
[/TR]
</tbody>[/TABLE]
[TABLE="width: 100"]
<tbody>[TR]
[TD]Type here to search[/TD]
[/TR]
</tbody>[/TABLE]
[TABLE="class: grid, width: 50"]
<tbody>[TR]
[TD]search button[/TD]
[/TR]
</tbody>[/TABLE]
[TABLE="class: grid, width: 50"]
<tbody>[TR]
[TD]clear button[/TD]
[/TR]
</tbody>[/TABLE]
below is the code i used:
Private Sub CommandButton1_Click()
Dim myButton As OptionButton
Dim SearchString As String
Dim ButtonName As String
Dim sht As Worksheet
Dim myField As Double
Dim DataRange As Range
Dim mySearch As Variant
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("A2:AP10000") 'Cell Range
'Retrieve User's Search Input
mySearch = sht.Shapes("UserSearch").TextFrame.Characters.Text 'Control Form
'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.Shapes("UserSearch").TextFrame.Characters.Text = "" 'Control Form
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
Private Sub CommandButton2_Click()
'PURPOSE: Clear all filter rules
'Clear filters on ActiveSheet
On Error Resume Next
ActiveSheet.ShowAllData
On Error GoTo 0
End Sub
i need to search and filter time also with numericals and text.
i need to search and filter time . it is searching text and numerical values. but when i search for time it is hiding the data.
below is my database and the code i used.
[TABLE="width: 201"]
<colgroup><col style="text-align: center;"><col style="text-align: center;"></colgroup><tbody>[TR]
[TD="align: center"]LINK[/TD]
[TD="align: center"]CLINK[/TD]
[/TR]
[TR]
[TD="align: center"]8:00:00[/TD]
[TD="align: center"]er[/TD]
[/TR]
[TR]
[TD="align: center"]GHJ[/TD]
[TD="align: center"]45[/TD]
[/TR]
[TR]
[TD="align: center"]SALIM[/TD]
[TD="align: center"]123[/TD]
[/TR]
[TR]
[TD="align: center"]SAIF[/TD]
[TD="align: center"]DF[/TD]
[/TR]
[TR]
[TD="align: center"]9:20:00[/TD]
[TD="align: center"]657[/TD]
[/TR]
[TR]
[TD="align: center"]10:20:00[/TD]
[TD="align: center"]78989[/TD]
[/TR]
[TR]
[TD="align: center"]10:35:00[/TD]
[TD="align: center"]900[/TD]
[/TR]
</tbody>[/TABLE]
[TABLE="width: 100"]
<tbody>[TR]
[TD]Type here to search[/TD]
[/TR]
</tbody>[/TABLE]
[TABLE="class: grid, width: 50"]
<tbody>[TR]
[TD]search button[/TD]
[/TR]
</tbody>[/TABLE]
[TABLE="class: grid, width: 50"]
<tbody>[TR]
[TD]clear button[/TD]
[/TR]
</tbody>[/TABLE]
below is the code i used:
Private Sub CommandButton1_Click()
Dim myButton As OptionButton
Dim SearchString As String
Dim ButtonName As String
Dim sht As Worksheet
Dim myField As Double
Dim DataRange As Range
Dim mySearch As Variant
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("A2:AP10000") 'Cell Range
'Retrieve User's Search Input
mySearch = sht.Shapes("UserSearch").TextFrame.Characters.Text 'Control Form
'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.Shapes("UserSearch").TextFrame.Characters.Text = "" 'Control Form
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
Private Sub CommandButton2_Click()
'PURPOSE: Clear all filter rules
'Clear filters on ActiveSheet
On Error Resume Next
ActiveSheet.ShowAllData
On Error GoTo 0
End Sub
i need to search and filter time also with numericals and text.