PC Review


Reply
Thread Tools Rate Thread

Autofilter not working with multiple field selections

 
 
signon77
Guest
Posts: n/a
 
      7th Nov 2008
The following code returns only one row of data after the header.

However range: rngColumnsToPopulate seems to only see the header row
not the row beneath it. Anyone know what might be wrong. It's the same
code I've used in the past when only one field has been used for
filtering criteria. Now I'm using 3 fields the code only seems to see
the header row.

With ws

.UsedRange.AutoFilter field:=colSource, Criteria1:="AED",
VisibleDropDown:=False
.UsedRange.AutoFilter field:=colMemo, Criteria1:="=",
VisibleDropDown:=False
.UsedRange.AutoFilter field:=colActionType, Criteria1:="=",
VisibleDropDown:=False

Set rngColumnsToPopulate
= .UsedRange.SpecialCells(xlCellTypeVisible, True)

End With
 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      7th Nov 2008
My guess is that you don't have blank cells in the colmemo and colactiontype
fields -- for the rows that have AED in then colsource field.

Or maybe the range is already filtered and hiding the rows you expected to
see???

With ws
.AutoFilterMode = False '<-- added to remove the autofilter.
.UsedRange.AutoFilter .......


ps. I'm not sure what the True does in .specialcells() portion, either. I'd
drop it.




signon77 wrote:
>
> The following code returns only one row of data after the header.
>
> However range: rngColumnsToPopulate seems to only see the header row
> not the row beneath it. Anyone know what might be wrong. It's the same
> code I've used in the past when only one field has been used for
> filtering criteria. Now I'm using 3 fields the code only seems to see
> the header row.
>
> With ws
>
> .UsedRange.AutoFilter field:=colSource, Criteria1:="AED",
> VisibleDropDown:=False
> .UsedRange.AutoFilter field:=colMemo, Criteria1:="=",
> VisibleDropDown:=False
> .UsedRange.AutoFilter field:=colActionType, Criteria1:="=",
> VisibleDropDown:=False
>
> Set rngColumnsToPopulate
> = .UsedRange.SpecialCells(xlCellTypeVisible, True)
>
> End With


--

Dave Peterson
 
Reply With Quote
 
signon77
Guest
Posts: n/a
 
      9th Nov 2008
On Nov 7, 6:16 pm, Dave Peterson <peter...@verizonXSPAM.net> wrote:
> My guess is that you don't have blank cells in the colmemo and colactiontype
> fields -- for the rows that have AED in then colsource field.
>
> Or maybe the range is already filtered and hiding the rows you expected to
> see???
>
> With ws
> .AutoFilterMode = False '<-- added to remove the autofilter.
> .UsedRange.AutoFilter .......
>
> ps. I'm not sure what the True does in .specialcells() portion, either. I'd
> drop it.
>
>
>
> signon77wrote:
>
> > The following code returns only one row of data after the header.

>
> > However range: rngColumnsToPopulate seems to only see the header row
> > not the row beneath it. Anyone know what might be wrong. It's the same
> > code I've used in the past when only one field has been used for
> > filtering criteria. Now I'm using 3 fields the code only seems to see
> > the header row.

>
> > With ws

>
> > .UsedRange.AutoFilter field:=colSource, Criteria1:="AED",
> > VisibleDropDown:=False
> > .UsedRange.AutoFilter field:=colMemo, Criteria1:="=",
> > VisibleDropDown:=False
> > .UsedRange.AutoFilter field:=colActionType, Criteria1:="=",
> > VisibleDropDown:=False

>
> > Set rngColumnsToPopulate
> > = .UsedRange.SpecialCells(xlCellTypeVisible, True)

>
> > End With

>
> --
>
> Dave Peterson


Hi Dave,

I've just tried writing the same code at home today with the same
problems:

The following is the data in my spreadsheet

first second
jdjj jkkjk
sss sdd
sss fda
sa fdfd
hah klk

The following is the code I'm wrting to test it:

Sub test()

Dim r As Range

With Sheets("Sheet2")
.AutoFilterMode = False
.UsedRange.AutoFilter field:=1, Criteria1:="hah"
.UsedRange.AutoFilter field:=2, Criteria1:="klk"

Set r = .UsedRange.SpecialCells(xlCellTypeVisible)

.UsedRange.AutoFilter
End With

End Sub

Problem: Even though both the header and one row of data are clearly
visible, range r only ever returns the header row (r.rows.count is
always = 1 instead of 2) .

Any idea what might be causing this problem?

Rob
 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      9th Nov 2008
Add
Msgbox R.address
And you'll see a couple of rows in the address.

R.rows.count will return the number of rows in the first area of the range. And
in your sample code with the sample data, the first area is a single row.

Try this with your same data:

Option Explicit
Sub test()

Dim r As Range

With Sheets("Sheet2")
.AutoFilterMode = False
.UsedRange.AutoFilter field:=1, Criteria1:="jdjj"
.UsedRange.AutoFilter field:=2, Criteria1:="jkkjk"

Set r = .UsedRange.SpecialCells(xlCellTypeVisible)
MsgBox r.Address

.UsedRange.AutoFilter
End With

End Sub

And you'll see two rows in the address--but only one area. And that means
you'll see 2 rows in the r.rows.count.

If you want to get the number of rows in the autofilter range (and set a
variable for just the details), you can do something like:

With activesheet.AutoFilter.Range
If .Columns(1).Cells.SpecialCells(xlCellTypeVisible).Count = 1 Then
MsgBox "only the headers are visible"
else
'resize to avoid the header
'and come down one row
Set VisRng = .Resize(.Rows.Count - 1).Offset(1, 0) _
.Cells.SpecialCells(xlCellTypeVisible)
End With

signon77 wrote:
>

<<snipped>>

> Hi Dave,
>
> I've just tried writing the same code at home today with the same
> problems:
>
> The following is the data in my spreadsheet
>
> first second
> jdjj jkkjk
> sss sdd
> sss fda
> sa fdfd
> hah klk
>
> The following is the code I'm wrting to test it:
>
> Sub test()
>
> Dim r As Range
>
> With Sheets("Sheet2")
> .AutoFilterMode = False
> .UsedRange.AutoFilter field:=1, Criteria1:="hah"
> .UsedRange.AutoFilter field:=2, Criteria1:="klk"
>
> Set r = .UsedRange.SpecialCells(xlCellTypeVisible)
>
> .UsedRange.AutoFilter
> End With
>
> End Sub
>
> Problem: Even though both the header and one row of data are clearly
> visible, range r only ever returns the header row (r.rows.count is
> always = 1 instead of 2) .
>
> Any idea what might be causing this problem?
>
> Rob


--

Dave Peterson
 
Reply With Quote
 
signon77
Guest
Posts: n/a
 
      10th Nov 2008
On Nov 9, 4:50*pm, Dave Peterson <peter...@verizonXSPAM.net> wrote:
> Add
> Msgbox R.address
> And you'll see a couple of rows in the address.
>
> R.rows.count will return the number of rows in the first area of the range. *And
> in your sample code with the sample data, the first area is a single row.
>
> Try this with your same data:
>
> Option Explicit
> Sub test()
>
> Dim r As Range
>
> With Sheets("Sheet2")
> * * .AutoFilterMode = False
> * * .UsedRange.AutoFilter field:=1, Criteria1:="jdjj"
> * * .UsedRange.AutoFilter field:=2, Criteria1:="jkkjk"
>
> * * Set r = .UsedRange.SpecialCells(xlCellTypeVisible)
> * * MsgBox r.Address
>
> * * .UsedRange.AutoFilter
> *End With
>
> End Sub
>
> And you'll see two rows in the address--but only one area. *And that means
> you'll see 2 rows in the r.rows.count.
>
> If you want to get the number of rows in the autofilter range (and set a
> variable for just the details), you can do something like:
>
> * * * * With activesheet.AutoFilter.Range
> * * * * * * If .Columns(1).Cells.SpecialCells(xlCellTypeVisible).Count = 1 Then
> * * * * * * * * MsgBox "only the headers are visible"
> * * * * * * else
> * * * * * * * *'resize to avoid the header
> * * * * * * * *'and come down one row
> * * * * * * * * Set VisRng = .Resize(.Rows.Count - 1).Offset(1, 0) _
> * * * * * * * * * * * * * * * *.Cells.SpecialCells(xlCellTypeVisible)
> * * * * End With
>
> signon77wrote:
>
> <<snipped>>
>
>
>
>
>
> > Hi Dave,

>
> > I've just tried writing the same code at home today with the same
> > problems:

>
> > The following is the data in my spreadsheet

>
> > first * second
> > jdjj * *jkkjk
> > sss * * sdd
> > sss * * fda
> > sa * * *fdfd
> > hah * * klk

>
> > The following is the code I'm wrting to test it:

>
> > Sub test()

>
> > Dim r As Range

>
> > With Sheets("Sheet2")
> > * .AutoFilterMode = False
> > * .UsedRange.AutoFilter field:=1, Criteria1:="hah"
> > * .UsedRange.AutoFilter field:=2, Criteria1:="klk"

>
> > * Set r = .UsedRange.SpecialCells(xlCellTypeVisible)

>
> > * * .UsedRange.AutoFilter
> > *End With

>
> > End Sub

>
> > Problem: Even though both the header and one row of data are clearly
> > visible, range r only ever returns the header row (r.rows.count is
> > always = 1 instead of 2) .

>
> > Any idea what might be causing this problem?

>
> > Rob

>
> --
>
> Dave Peterson- Hide quoted text -
>
> - Show quoted text -


Dave,

Thanks a lot.

I had no idea that r.rows.count would ever only return data in the
first row because r is actually a range comprised of multiple areas.

This painful lesson has been worth it.

Rob
 
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
Multiple Selections for Single Field oftenconfused Microsoft Access 1 12th Jan 2009 03:58 PM
PT - list multiple page field selections rachael Microsoft Excel Programming 0 3rd Jan 2008 09:21 PM
Multiple selections in a form field drop-down list =?Utf-8?B?TXMuRA==?= Microsoft Word Document Management 1 12th Jul 2006 05:40 PM
List box: Multiple selections store in a field SF Microsoft Access Database Table Design 3 24th Feb 2006 07:39 PM
How do I have multiple selections in a look up field in access? =?Utf-8?B?a2VybmFseg==?= Microsoft Access Database Table Design 1 1st Mar 2005 05:29 AM


Features
 

Advertising
 

Newsgroups
 


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