PC Review


Reply
Thread Tools Rate Thread

Autofilter with optional criteria

 
 
Basta1980
Guest
Posts: n/a
 
      17th Jul 2009
Hi there,

I'm using an userform which gives an user the oppurtunity to fill in data
which is used to run the autofilter. There are 4 boxes the user can fill in.
One box (combobox1) is not optional, the rest (all the textboxes) is optional
i.e. the user gets to choose if they fill in the designated boxes or not. The
code below works when all boxes are filled in, but how do I get them to be
optional

Private Sub CommandButton1_Click()

TextBox1.SetFocus
Worksheets(ComboBox1.Value).Range("A1").AutoFilter _
field:=1, _
Criteria1:=TextBox1.Value
Worksheets(ComboBox1.Value).Range("A1").AutoFilter _
field:=5, _
Criteria1:=TextBox2.Value
Worksheets(ComboBox1.Value).Range("A1").AutoFilter _
field:=7, _
Criteria1:=TextBox3.Value
Worksheets(ComboBox1.Value).Activate

End Sub

Thnx in advance,

Basta1980
 
Reply With Quote
 
 
 
 
Jacob Skaria
Guest
Posts: n/a
 
      17th Jul 2009
One way is to use IF statement..

If Trim(TextBox2.Value) <> "" Then
Worksheets(ComboBox1.Value).Range("A1").AutoFilter _
field:=5, Criteria1:=TextBox2.Value
End If

If this post helps click Yes
---------------
Jacob Skaria


"Basta1980" wrote:

> Hi there,
>
> I'm using an userform which gives an user the oppurtunity to fill in data
> which is used to run the autofilter. There are 4 boxes the user can fill in.
> One box (combobox1) is not optional, the rest (all the textboxes) is optional
> i.e. the user gets to choose if they fill in the designated boxes or not. The
> code below works when all boxes are filled in, but how do I get them to be
> optional
>
> Private Sub CommandButton1_Click()
>
> TextBox1.SetFocus
> Worksheets(ComboBox1.Value).Range("A1").AutoFilter _
> field:=1, _
> Criteria1:=TextBox1.Value
> Worksheets(ComboBox1.Value).Range("A1").AutoFilter _
> field:=5, _
> Criteria1:=TextBox2.Value
> Worksheets(ComboBox1.Value).Range("A1").AutoFilter _
> field:=7, _
> Criteria1:=TextBox3.Value
> Worksheets(ComboBox1.Value).Activate
>
> End Sub
>
> Thnx in advance,
>
> Basta1980

 
Reply With Quote
 
Basta1980
Guest
Posts: n/a
 
      17th Jul 2009
Jacob,

Thanks, but will this setup make it possible to leave textbox 3 and 4 blank
and still return data?!

regards

"Jacob Skaria" wrote:

> One way is to use IF statement..
>
> If Trim(TextBox2.Value) <> "" Then
> Worksheets(ComboBox1.Value).Range("A1").AutoFilter _
> field:=5, Criteria1:=TextBox2.Value
> End If
>
> If this post helps click Yes
> ---------------
> Jacob Skaria
>
>
> "Basta1980" wrote:
>
> > Hi there,
> >
> > I'm using an userform which gives an user the oppurtunity to fill in data
> > which is used to run the autofilter. There are 4 boxes the user can fill in.
> > One box (combobox1) is not optional, the rest (all the textboxes) is optional
> > i.e. the user gets to choose if they fill in the designated boxes or not. The
> > code below works when all boxes are filled in, but how do I get them to be
> > optional
> >
> > Private Sub CommandButton1_Click()
> >
> > TextBox1.SetFocus
> > Worksheets(ComboBox1.Value).Range("A1").AutoFilter _
> > field:=1, _
> > Criteria1:=TextBox1.Value
> > Worksheets(ComboBox1.Value).Range("A1").AutoFilter _
> > field:=5, _
> > Criteria1:=TextBox2.Value
> > Worksheets(ComboBox1.Value).Range("A1").AutoFilter _
> > field:=7, _
> > Criteria1:=TextBox3.Value
> > Worksheets(ComboBox1.Value).Activate
> >
> > End Sub
> >
> > Thnx in advance,
> >
> > Basta1980

 
Reply With Quote
 
Jacob Skaria
Guest
Posts: n/a
 
      17th Jul 2009
Do you mean..the below

Dim ws As Worksheet
Dim rngTemp As Range
Set ws = Worksheets(ComboBox1.Value)
Set rngTemp = ws.Range("A1")
rngTemp.AutoFilter Field:=1, Criteria1:=TextBox1.Value
If Trim(textbox2.Value) <> "" Then _
rngTemp.AutoFilter Field:=5, Criteria1:=textbox2.Value
If Trim(TextBox3.Value) <> "" Then _
rngTemp.AutoFilter Field:=7, Criteria1:=TextBox3.Value
If Trim(TextBox4.Value) <> "" Then _
rngTemp.AutoFilter Field:=9, Criteria1:=TextBox4.Value
ws.Activate

If this post helps click Yes
---------------
Jacob Skaria


"Basta1980" wrote:

> Jacob,
>
> Thanks, but will this setup make it possible to leave textbox 3 and 4 blank
> and still return data?!
>
> regards
>
> "Jacob Skaria" wrote:
>
> > One way is to use IF statement..
> >
> > If Trim(TextBox2.Value) <> "" Then
> > Worksheets(ComboBox1.Value).Range("A1").AutoFilter _
> > field:=5, Criteria1:=TextBox2.Value
> > End If
> >
> > If this post helps click Yes
> > ---------------
> > Jacob Skaria
> >
> >
> > "Basta1980" wrote:
> >
> > > Hi there,
> > >
> > > I'm using an userform which gives an user the oppurtunity to fill in data
> > > which is used to run the autofilter. There are 4 boxes the user can fill in.
> > > One box (combobox1) is not optional, the rest (all the textboxes) is optional
> > > i.e. the user gets to choose if they fill in the designated boxes or not. The
> > > code below works when all boxes are filled in, but how do I get them to be
> > > optional
> > >
> > > Private Sub CommandButton1_Click()
> > >
> > > TextBox1.SetFocus
> > > Worksheets(ComboBox1.Value).Range("A1").AutoFilter _
> > > field:=1, _
> > > Criteria1:=TextBox1.Value
> > > Worksheets(ComboBox1.Value).Range("A1").AutoFilter _
> > > field:=5, _
> > > Criteria1:=TextBox2.Value
> > > Worksheets(ComboBox1.Value).Range("A1").AutoFilter _
> > > field:=7, _
> > > Criteria1:=TextBox3.Value
> > > Worksheets(ComboBox1.Value).Activate
> > >
> > > End Sub
> > >
> > > Thnx in advance,
> > >
> > > Basta1980

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
2 Forms - Optional criteria, is it possible ? randria Microsoft Access Queries 3 3rd Feb 2009 05:12 PM
Selection.AutoFilter Field / Criteria => criteria sometimes non-existing on worksheet markx Microsoft Excel Programming 1 24th Nov 2006 02:52 PM
Query with mulitple optional criteria =?Utf-8?B?cGFsdG8x?= Microsoft Access Queries 4 22nd Nov 2004 06:11 PM
Optional Criteria =?Utf-8?B?SlBlYWtl?= Microsoft Access Forms 0 25th Mar 2004 04:46 PM
Forgotten how to do optional criteria in a report Sandy H Microsoft Access Form Coding 0 10th Jul 2003 04:55 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:18 AM.