View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
strive4peace[_2_] strive4peace[_2_] is offline
external usenet poster
 
Posts: 7
Default Filtering and user input

you're welcome, Lvenom :) happy to help

you could also make the sub more generic by sending the
operator, field number, and InputBox message as
parameters... that way, you can use it for other columns and
other conditions...

'~~~~~~~~~~~~
ie:
button code behind the sheet

'~~~~~~~~~~~~
Private Sub IsEqual_Click()
filterOnOff "=", 12, _
"Enter Scheduled Downday to Match (MM/DD/YY)"
End Sub

Private Sub IsLessThanOrEqual_Click()
filterOnOff "<=", 12, _
"Enter Last Scheduled Downday to Show (MM/DD/YY)"
End Sub

Private Sub IsNotEqual_Click()
filterOnOff "<", 12, _
"Enter Scheduled Downday to Exclude (MM/DD/YY)"
End Sub
'~~~~~~~~~~~~

general module

'~~~~~~~~~~~~
Sub filterOnOff( _
pOperator As String, _
pFieldNum As Integer, _
pMsg As String)

'written by Crystal
'strive4peace2007 at yahoo.com

'PARAMETERS
'pOperator -- "=", "<" ... any valid operator
'pFieldNum -- Field number in AutoFilter
'assume field corresponds to column for err 2004
'pMsg -- message for InputBox

On Error GoTo Proc_Err

Dim FilterCriteria As String, mMsg As String

FilterCriteria = InputBox(pMsg)

'user clicked Cancel
If FilterCriteria = "" Then Exit Sub

Selection.AutoFilter _
Field:=pFieldNum, _
Criteria1:=pOperator & FilterCriteria

Proc_Exit:
On Error Resume Next
Exit Sub

Proc_Err:
Select Case Err.Number
Case 1004
'turn on autofilter
'assume row 2 is valid
'assume Field number is column number
Range(Cells(2, pFieldNum)).Select
Selection.AutoFilter

Resume
Case Else
MsgBox Err.Description, _
, "ERROR " & Err.Number _
& " filterOnOff"
'press F8 to step through code and debug
'remove next line after debugged
Stop: Resume
End Select
Resume Proc_Exit
End Sub
'~~~~~~~~~~~~

if you really want to get fancy, you could use an option
group to set the operator (=, <, <=, =, etc) and combine
your code even more...




Warm Regards,
Crystal
Microsoft Access MVP 2006

*
Have an awesome day ;)

remote programming and training
strive4peace2006 at yahoo.com

*

Lvenom wrote:
Thanks to both of you. The simple fix supplied by Executor worked quite
well and does give the results I expect. I will keep your suggestion on
file strive4peace in case the bugs start coming out. again thanks to
both of you for your quick response.