Mysterious Dissapearing Autofilters

Craw

New Member
Joined
Jul 27, 2011
Messages
32
Hey All,
I have a series of command buttons in a spreadsheet that initiate an inputbox to search various autofilters for user defined queries. The problem is if I hit cancel on the input box all the autofilters on the sheet dissappear...I have included a couple examples of the code I am using for to manage the user defined queries.

Example 1
Code:
Sub daterange()
'
' daterange Macro
'
Dim strName1 As Variant, strName2 As Variant
Dim caption As String
Dim Prompt As String
 
Prompt = ""
caption = "Approval Date Range Search"
On Error GoTo Errorline
 
strName1 = DateValue(Application.InputBox(Prompt & " Enter 'START' date (dd/mm/yyyy)", caption, Type:=2))
strName2 = DateValue(Application.InputBox(Prompt & " Enter 'END' date (dd/mm/yyyy)", 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

Example 2
Code:
Sub ksearch()
'
' ksearch Macro
'
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

How can I prevent the autofilters from dissappearing when a user hits cancel on the inputbox?
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
In Example2, code Selection.AutoFilter toggles the filter. If the range has filters on already, it will turn it off. Removing the statement altogether will fix your problem. In essence you are "doing nothing" if inputbox is null.

In Example1, you are not checking for condition where the inputbox returns blanks. Adding the red statement should solve your problem.
Rich (BB code):
strName1 = DateValue(Application.InputBox(Prompt & " Enter 'START' date (dd/mm/yyyy)", Caption, Type:=2))
strName2 = DateValue(Application.InputBox(Prompt & " Enter 'END' date (dd/mm/yyyy)", Caption, Type:=2))
If strName1 = "" Or strName2 = "" Then Exit Sub
If strName1 = "False" Or strName2 = "False" Then Exit Sub
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,833
Members
452,947
Latest member
Gerry_F

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