Accessing the AutoFilter method via VB.net

K

Keith Howard

There seems to be some inconsistency between the Excel VBA and the Excel Dot
Net object models. I implemented auto filtering via the following code in
Excel VBA:
Selection.AutoFilter
However, when I tried to do the same thing in Dot Net, via the following
code, the AutoFilter object was not available, causing a compilation error:
Friend Shared Sub gsxxxAutoFilter(ByVal vvwksActive As Worksheet)
vvwksActive.Cells.Select()
vvwksActive.selection 'This does not compile because the selection
method does not exist.
vvwksActive.Application.Selection.autofilter() 'This does not
compile because the autofilter method does not exist, even though I was able
to access the Selection object via the Application object.
End Sub
Does anyone know how to access the AutoFilter method via Dot Net?
Thanks in advance.
Keith
 
J

Jacob Skaria

Without using Selection, try

Friend Shared Sub gsxxxAutoFilter(ByVal vvwksActive As Worksheet)
vvwksActive.Cells.AutoFilter
End Sub
 
K

Keith Howard

Jacob,
Thanks for the fast response.
In VBA, your approach has the effect of applying AutoFilter to row 1, which
is not what I am trying to do. I am trying to apply an AutoFilter starting
from Row 7 (which is where my selection is).
I have also tried to create a named range and to apply an AutoFilter thereto
via the following code:
Range("TestRange").AutoFilter
This does not work; it does nothing as far as I can tell. No AutoFilter has
been applied.
Any other ideas? Thanks in advance. (I did not know that the Excel Dot Net
object model was different from the VBA one!)
Regards,
Keith
 
J

Jacob Skaria

Try

vvwksActive.Range("A7:" &
split(vvwksActive.UsedRange.Address,":")(1)).AutoFilter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top