Search by approval date coding issue

Craw

New Member
Joined
Jul 27, 2011
Messages
32
Greetings,
I am trying to get this code to work. It works when i'm searching for text but I can't seem to get it to search for approval dates (dd-mm-yyyy) that are in a spreadsheet i created.

Private Sub approvalsearch_Click()

Dim strName As String
Dim caption As String
Dim Prompt As String

Prompt = "What Approval Date Would You Like to Search For?"
caption = "Approval Date Search"
strName = Val(InputBox(Prompt, caption))

Range("Table_owssvr_1[Approval Date]").Select
Selection.AutoFilter Field:=6, Criteria1:="=*" & strName & ", Operator:=xlAnd"

End Sub

Any ideas on how to fix it?
 
Nevermind I got it to work I was missing the last line of code it didnt copy for reason. Once again my hat is off to you.

Thanks Much,

Craw
 
Upvote 0

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Well I got the code to work after all thank you for all your help. But now I have one last question. The code I used to get the Date Range subroutine to work is as follows:

Private Sub CommandButton13_Click()

Dim strName1 As Variant, strName2 As Variant
Dim caption As String
Dim Prompt As String


Prompt = "Enter the Date Range in the 'dd/mm/yyyy' format?"
caption = "Approval Date Range Search"
On Error Resume Next


strName1 = DateValue(InputBox(Prompt & " Enter 'START' Date", caption))
strName2 = DateValue(InputBox(Prompt & " Enter 'END' Date", caption))


Range("Table_owssvr_1[Approval Date]").Select
Selection.AutoFilter Field:=6, Criteria1:=">=" & CLng(strName1), Criteria2:="<=" & CLng(strName2), Operator:=xlAnd

End Sub

How would I code this to use an application inputbox so that when the cancel button is hit the command is ended without filtering any data and if the user leaves the inputbox blank the command will look for blank values. As some of the documents in my last have not been approved and searching for blank columns would be helpful.

Best Regards,


Craw
 
Upvote 0
Maybe

Code:
Private Sub CommandButton13_Click()

Dim strName1 As Variant, strName2 As Variant
Dim caption As String
Dim Prompt As String

Prompt = "Enter the Date Range in the 'dd/mm/yyyy' format?"
caption = "Approval Date Range Search"
On Error Resume Next

strName1 = DateValue(Application.InputBox(Prompt & " Enter 'START' Date", caption, Type:=2))
strName2 = DateValue(Application.InputBox(Prompt & " Enter 'END' Date", caption, Type:=2))
If strName1 = "False" Or strName2 = "False" Then Exit Sub
Range("Table_owssvr_1[Approval Date]").Select
Selection.AutoFilter Field:=6, Criteria1:=">=" & CLng(strName1), Criteria2:="<=" & CLng(strName2), Operator:=xlAnd

End Sub
 
Upvote 0
if nothing is entered it just filters everything out and no values are shown so I have to clear the filter. Is there a way to at least make it clear the filter on error?

the filter clearing code I have is as follows:

ActiveSheet.ListObjects("Table_owssvr_1").Range.AutoFilter Field:=6

I'm just not sure where to put it. I have tried putting it after the error handling command but then it still filters everything out?
 
Upvote 0
Actually is there a way to make the errorline I have added be skipped if their is no error?

Private Sub CommandButton13_Click()
Dim strName1 As Variant, strName2 As Variant
Dim caption As String
Dim Prompt As String


Prompt = "Enter the Date Range in the 'dd/mm/yyyy' format?"
caption = "Approval Date Range Search"

On Error GoTo ErrorLine

strName1 = DateValue(Application.InputBox(Prompt & " Enter 'START' Date", caption, Type:=2))
strName2 = DateValue(Application.InputBox(Prompt & " Enter 'END' Date", caption, Type:=2))

If strName1 = "False" Or strName2 = "False" Then Exit Sub
Range("Table_owssvr_1[Approval Date]").Select
Selection.AutoFilter Field:=6, Criteria1:=">=" & CLng(strName1), Criteria2:="<=" & CLng(strName2), Operator:=xlAnd


ErrorLine:
ActiveSheet.ListObjects("Table_owssvr_1").Range.AutoFilter Field:=6

End Sub
 
Upvote 0
Try

Rich (BB code):
Private Sub CommandButton13_Click()
Dim strName1 As Variant, strName2 As Variant
Dim caption As String
Dim Prompt As String

Prompt = "Enter the Date Range in the 'dd/mm/yyyy' format?"
caption = "Approval Date Range Search"

On Error GoTo ErrorLine

strName1 = DateValue(Application.InputBox(Prompt & " Enter 'START' Date", caption, Type:=2))
strName2 = DateValue(Application.InputBox(Prompt & " Enter 'END' Date", caption, Type:=2))

If strName1 = "False" Or strName2 = "False" Then Exit Sub
Range("Table_owssvr_1[Approval Date]").Select
Selection.AutoFilter Field:=6, Criteria1:=">=" & CLng(strName1), Criteria2:="<=" & CLng(strName2), Operator:=xlAnd
Exit Sub
ErrorLine:
ActiveSheet.ListObjects("Table_owssvr_1").Range.AutoFilter Field:=6

End Sub
 
Upvote 0
I have a very similar issue with some of my other functions. I would like to arrange it so that if the user does not enter a value or hits cancel it does not filter the autofilter by 0 rather that it clears the filter automatically. Any suggestions?

Private Sub CommandButton10_Click()

Dim strName As String
Dim caption As String
Dim Prompt As String


Prompt = "Enter keyword"
caption = "Keyword Search"

strName = (InputBox(Prompt, caption))
Range("Table_owssvr_1[Keywords]").Select
Selection.AutoFilter Field:=5, Criteria1:="=*" & strName & "*", Operator:=xlAnd

End Sub
 
Upvote 0
Maybe like this

Code:
Private Sub CommandButton10_Click()

Dim strName As String
Dim caption As String
Dim Prompt As String

Prompt = "Enter keyword"
caption = "Keyword Search"

strName = (InputBox(Prompt, caption))
Range("Table_owssvr_1[Keywords]").Select
If strName = "" Then
    Selection.AutoFilter
Else
    Selection.AutoFilter Field:=5, Criteria1:="=*" & strName & "*", Operator:=xlAnd
End If
End Sub

Please use Code Tags when posting code

[code]
your code here
[/code]
 
Upvote 0
Hey,
I would like to modify this code for another function so that when the button is clicked it will automatically check the current date and look for records that go back 1 year or more. Is this possible?

Code:
Private Sub CommandButton5_Click()
 
Dim strName1 As Variant, strName2 As Variant
Dim caption As String
Dim Prompt As String
 
Prompt = "Enter the Date Range in the 'dd/mm/yyyy' format?"
caption = "Approval Date Range Search"
On Error GoTo Errorline
 
strName1 = DateValue(Application.InputBox(Prompt & " Enter 'START' Date", caption, Type:=2))
strName2 = DateValue(Application.InputBox(Prompt & " Enter 'END' Date", caption, Type:=2))
If strName1 = "False" Or strName2 = "False" Then Exit Sub

Range("Table_owssvr_1[Approval Date]").Select
Selection.AutoFilter Field:=6, Criteria1:=">=" & CLng(strName1), Criteria2:="<=" & CLng(strName2), Operator:=xlAnd
Exit Sub

Errorline:
ActiveSheet.ListObjects("Table_owssvr_1").Range.AutoFilter Field:=6
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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