carlrubber
New Member
- Joined
- Oct 15, 2016
- Messages
- 48
I have a cell G1 storing a date and I would like to filter by changing the date in G1. however the below code not filter the correct answer.
would anyone help please....
would anyone help please....
VBA Code:
Sub SearchBox()
'PURPOSE: Filter Data on User-Determined Column & Text/Numerical value
Dim myButton As OptionButton
Dim SearchDate As String
Dim ButtonName As String
Dim sht As Worksheet
Dim myField As Long
Dim DataRange As Range
Dim mySearch As Variant
Set sht = ActiveSheet
'Unfilter Data (if necessary)
On Error Resume Next
ActiveSheet.ShowAllData
On Error GoTo 0
'Filtered Data Range
Set DataRange = Range("A30:k1003")
'Retrieve User's Search Input
mySearch = Sheets("Table 1&2").Range("G1")
mySearch = CLng(DateValue(mySearch))
'Determine if user is searching for number or text
If IsNumeric(mySearch) = True Then
SearchDate = "=*" & mySearch
'Else
' SearchString = "=*" & mySearch & "*"
End If
'Loop Through Option Buttons
For Each myButton In ActiveSheet.OptionButtons
If myButton.Value = 1 Then
ButtonName = myButton.Text
Exit For
End If
Next myButton
'Determine Filter Field
myField = WorksheetFunction.Match(ButtonName, DataRange.Rows(1), 0)
'Filter Data
DataRange.AutoFilter _
Field:=myField, _
Criteria1:=SearchDate, _
Operator:=xlAnd
End Sub