AutoFilter method of Range class failed

M

maboli

The first version of creating an autofilter in Excel works fine, but why do I
get an
exception on the second version?

I am coding in VB.NET (VB 2005).
(I would like to compile my code with option strict set to ON and then only
the second version does compile)

...
excelWorksheet.Activate()

' VERSION 1: the following 3 lines work fine
Dim range1 As Excel.Range = excelWorksheet.Range("A:A")
range1.Select()
excelWorksheet.Application.Selection.AutoFilter()


' VERSION 2: throws an Exceptions
Dim range2 As Excel.Range = CType(excelWorksheet.Range("B:B"),
Excel.Range)
range2.Select()
Dim rangeSel As Excel.Range =
CType(excelWorksheet.Application.Selection, Excel.Range)
rangeSel.AutoFilter() ' >> Exception: "AutoFilter method of Range
class failed"

Help would be appreciated!
 
M

maboli

Thanks for the answer.

But there is data in column B. It fails all the same when I change version 2
to use column A.
Do you have other ideas why version 2 fails?

Regards,
Manfred
 

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