Excel AutoFilter


New member
May 30, 2013
Programming Experience
This has been driving me nuts all day and hopefully someone here knows a solution.

Using VB2010, .NET v4.0 framework, have a reference to the "Microsoft Excel12.0 Object Library" set (working with Excel 2007) and have the Microsoft.Office.Interop imported and all of my other code to format an Excel spreadsheet works.

That is until I get to the point where I want to set an Autofilter on a range. Then I get an "AutoFilter method of Range class failed" error.

Now, I do have "Strict" turned on and due to some other issues I have to leave it on otherwise this wouldn't be an issue.

My little code snippit looks like this...

Private Sub Format(ByVal Statement_Worksheet As Excel.Worksheet)
Dim Worksheet_AutoFilter_Range As Excel.Range = Statement_Worksheet.Range("$B$15:$O$15")


<So Some Excel Formatting code>

CType(Statement_Worksheet.Application.Selection, Excel.Range).AutoFilter()

I had to "CType" the "Statement_Worksheet.Application.Selection" as an Excel.Range since "Strict" is on. I've searched all over the internet and except my "CType" what I have appears to be how to do it, but it keeps crashing out.

So what am I doing wrong, am I casting it wrong?

Any and all help would be appreciated with this as I've been dealing with this too long and my head hurts.
Top Bottom