I am trying to create a workbook where one of the worksheets contains
an AutoFiltered table. It looks like it should be simple -- what
I am doing is this:
rng = ws.Range("MyTableRange")
rng.AutoFilter()
Unfortunately I get an exception on the second statement, with the
unhelpful message:
AutoFilter method of Range class failed
The range is rectangular, and contains a legitimate table, with
column headers in the first row and data in most of the cells. If
I create the workbook without calling the AutoFilter statement,
then manually go to the range in question and set AutoFilter from
the Excel Data menu, it works as expected. The worksheet is intended
to be protected, but protection has not yet been set at the point
where I try to set AutoFilter.
I Googled for this, and found two other people complaining of the
same thing, with no answers. I suspect (and hope!) this means that
there is an answer -- otherwise there would be many more queries.
Any help would be gratefully appreciated!
--
John Brock jb****@panix.com 9 13685
Hi
See this page http://www.contextures.com/xlautofilter03.html
--
Regards Ron De Bruin http://www.rondebruin.nl
"John Brock" <jb****@panix.com> wrote in message news:e6**********@reader1.panix.com... I am trying to create a workbook where one of the worksheets contains an AutoFiltered table. It looks like it should be simple -- what I am doing is this:
rng = ws.Range("MyTableRange") rng.AutoFilter()
Unfortunately I get an exception on the second statement, with the unhelpful message:
AutoFilter method of Range class failed
The range is rectangular, and contains a legitimate table, with column headers in the first row and data in most of the cells. If I create the workbook without calling the AutoFilter statement, then manually go to the range in question and set AutoFilter from the Excel Data menu, it works as expected. The worksheet is intended to be protected, but protection has not yet been set at the point where I try to set AutoFilter.
I Googled for this, and found two other people complaining of the same thing, with no answers. I suspect (and hope!) this means that there is an answer -- otherwise there would be many more queries.
Any help would be gratefully appreciated! -- John Brock jb****@panix.com
Your syntax look a little off...
set rng = ws.Range("MyTableRange")
rng.AutoFilter
Give that a try.
--
HTH...
Jim Thomlinson
"John Brock" wrote: I am trying to create a workbook where one of the worksheets contains an AutoFiltered table. It looks like it should be simple -- what I am doing is this:
rng = ws.Range("MyTableRange") rng.AutoFilter()
Unfortunately I get an exception on the second statement, with the unhelpful message:
AutoFilter method of Range class failed
The range is rectangular, and contains a legitimate table, with column headers in the first row and data in most of the cells. If I create the workbook without calling the AutoFilter statement, then manually go to the range in question and set AutoFilter from the Excel Data menu, it works as expected. The worksheet is intended to be protected, but protection has not yet been set at the point where I try to set AutoFilter.
I Googled for this, and found two other people complaining of the same thing, with no answers. I suspect (and hope!) this means that there is an answer -- otherwise there would be many more queries.
Any help would be gratefully appreciated! -- John Brock jb****@panix.com
In article <Oq**************@TK2MSFTNGP04.phx.gbl>,
Ron de Bruin <ro********@kabelfoon.nl> wrote: Hi
See this page http://www.contextures.com/xlautofilter03.html
The VBA (not VB.NET) example on that page is:
Sub TurnAutoFilterOn()
'check for filter, turn on if none exists
If Not ActiveSheet.AutoFilterMode Then
ActiveSheet.Range("A1").AutoFilter
End If
End Sub
I don't see how this is different than what I am doing now. My
'ws' variable *is* the active worksheet (in fact the only worksheet).
I tried using a single cell range, as in the example, but that
makes no difference. The AutoFilter statement still throws an
exception.
The example given for turning off AutoFiltering uses an AutoFilterMode
variable. I changed my code to:
ws.AutoFilterMode = true
rng = ws.Range("MyTableRange")
rng.AutoFilter()
But now the first statement throws an exception, with the even less
enlightening message:
Exception from HRESULT: 0x800A03EC.
There is also a ws.FilterMode, which is read-only, so I can't do
anything with it, and a ws.EnableAutoFilter variable, which looks
promising, and which I can successfully set true, but which doesn't
seem to make any difference.
Come on, *somebody* must have done this!
"John Brock" <jb****@panix.com> wrote in message news:e6**********@reader1.panix.com...I am trying to create a workbook where one of the worksheets contains an AutoFiltered table. It looks like it should be simple -- what I am doing is this:
rng = ws.Range("MyTableRange") rng.AutoFilter()
Unfortunately I get an exception on the second statement, with the unhelpful message:
AutoFilter method of Range class failed
The range is rectangular, and contains a legitimate table, with column headers in the first row and data in most of the cells. If I create the workbook without calling the AutoFilter statement, then manually go to the range in question and set AutoFilter from the Excel Data menu, it works as expected. The worksheet is intended to be protected, but protection has not yet been set at the point where I try to set AutoFilter.
I Googled for this, and found two other people complaining of the same thing, with no answers. I suspect (and hope!) this means that there is an answer -- otherwise there would be many more queries.
Any help would be gratefully appreciated! -- John Brock jb****@panix.com
--
John Brock jb****@panix.com
Sorry, can't test in on this moment for you
I have a new machine and not have VB.NET installed on this moment
--
Regards Ron De Bruin http://www.rondebruin.nl
"John Brock" <jb****@panix.com> wrote in message news:e6**********@reader1.panix.com... In article <Oq**************@TK2MSFTNGP04.phx.gbl>, Ron de Bruin <ro********@kabelfoon.nl> wrote:Hi
See this page http://www.contextures.com/xlautofilter03.html
The VBA (not VB.NET) example on that page is:
Sub TurnAutoFilterOn() 'check for filter, turn on if none exists If Not ActiveSheet.AutoFilterMode Then ActiveSheet.Range("A1").AutoFilter End If End Sub
I don't see how this is different than what I am doing now. My 'ws' variable *is* the active worksheet (in fact the only worksheet). I tried using a single cell range, as in the example, but that makes no difference. The AutoFilter statement still throws an exception.
The example given for turning off AutoFiltering uses an AutoFilterMode variable. I changed my code to:
ws.AutoFilterMode = true rng = ws.Range("MyTableRange") rng.AutoFilter()
But now the first statement throws an exception, with the even less enlightening message:
Exception from HRESULT: 0x800A03EC.
There is also a ws.FilterMode, which is read-only, so I can't do anything with it, and a ws.EnableAutoFilter variable, which looks promising, and which I can successfully set true, but which doesn't seem to make any difference.
Come on, *somebody* must have done this!
"John Brock" <jb****@panix.com> wrote in message news:e6**********@reader1.panix.com...I am trying to create a workbook where one of the worksheets contains an AutoFiltered table. It looks like it should be simple -- what I am doing is this:
rng = ws.Range("MyTableRange") rng.AutoFilter()
Unfortunately I get an exception on the second statement, with the unhelpful message:
AutoFilter method of Range class failed
The range is rectangular, and contains a legitimate table, with column headers in the first row and data in most of the cells. If I create the workbook without calling the AutoFilter statement, then manually go to the range in question and set AutoFilter from the Excel Data menu, it works as expected. The worksheet is intended to be protected, but protection has not yet been set at the point where I try to set AutoFilter.
I Googled for this, and found two other people complaining of the same thing, with no answers. I suspect (and hope!) this means that there is an answer -- otherwise there would be many more queries.
Any help would be gratefully appreciated! -- John Brock jb****@panix.com
-- John Brock jb****@panix.com
Jim Thomlinson wrote: Your syntax look a little off...
set rng = ws.Range("MyTableRange") rng.AutoFilter
No. In VB.NET, the "Set" and "Let" keywords are not supported.
Therefore, when you are setting a reference to an object, you can't
type "Set x = y" anymore; instead you type "x = y", same as a normal
assignment. VB.NET code that uses "Set" or "Let" will not compile;
however, if you are using the Visual Studio .NET IDE to write your
code, the IDE will simply delete any "Set"s or "Let"s if you try to
type them, in order to save you from writing code that won't compile.
--
Mike S
John Brock wrote: In article <Oq**************@TK2MSFTNGP04.phx.gbl>, Ron de Bruin <ro********@kabelfoon.nl> wrote:Hi
See this page http://www.contextures.com/xlautofilter03.html
The VBA (not VB.NET) example on that page is:
Sub TurnAutoFilterOn() 'check for filter, turn on if none exists If Not ActiveSheet.AutoFilterMode Then ActiveSheet.Range("A1").AutoFilter End If End Sub
I don't see how this is different than what I am doing now. My 'ws' variable *is* the active worksheet (in fact the only worksheet). I tried using a single cell range, as in the example, but that makes no difference. The AutoFilter statement still throws an exception.
The example given for turning off AutoFiltering uses an AutoFilterMode variable. I changed my code to:
ws.AutoFilterMode = true rng = ws.Range("MyTableRange") rng.AutoFilter()
But now the first statement throws an exception, with the even less enlightening message:
Exception from HRESULT: 0x800A03EC.
Lifted from the MSDN page on AutoFilterMode at http://msdn2.microsoft.com/en-us/mic...ltermode.aspx:
Remarks
You can set this property to false to remove the arrows, but you
cannot set it to true.
That would explain why the line 'ws.AutoFilterMode = true' throws an
exception.
I'm not very familiar with Excel programming, but in every example I
could find, everyone always sets AutoFilterMode to false (which is
allowed) before calling AutoFilter on their range object. So I would
say try this:
ws.AutoFilterMode = false
rng = ws.Range("MyTableRange")
rng.AutoFilter()
Setting AutoFilterMode to false also has the side-effect of removing
any previous AutoFilters that were on the worksheet, which I guess is
important before you call AutoFilter() again...
<snip>
Mike S
Sorry. I missed the title of the post...
--
HTH...
Jim Thomlinson
"Mike S" wrote: Jim Thomlinson wrote: Your syntax look a little off...
set rng = ws.Range("MyTableRange") rng.AutoFilter
No. In VB.NET, the "Set" and "Let" keywords are not supported. Therefore, when you are setting a reference to an object, you can't type "Set x = y" anymore; instead you type "x = y", same as a normal assignment. VB.NET code that uses "Set" or "Let" will not compile; however, if you are using the Visual Studio .NET IDE to write your code, the IDE will simply delete any "Set"s or "Let"s if you try to type them, in order to save you from writing code that won't compile.
-- Mike S
In article <e6**********@reader1.panix.com>,
John Brock <jb****@panix.com> wrote: I am trying to create a workbook where one of the worksheets contains an AutoFiltered table. It looks like it should be simple -- what I am doing is this:
rng = ws.Range("MyTableRange") rng.AutoFilter()
Unfortunately I get an exception on the second statement, with the unhelpful message:
AutoFilter method of Range class failed
The range is rectangular, and contains a legitimate table, with column headers in the first row and data in most of the cells. If I create the workbook without calling the AutoFilter statement, then manually go to the range in question and set AutoFilter from the Excel Data menu, it works as expected. The worksheet is intended to be protected, but protection has not yet been set at the point where I try to set AutoFilter.
I Googled for this, and found two other people complaining of the same thing, with no answers. I suspect (and hope!) this means that there is an answer -- otherwise there would be many more queries.
Any help would be gratefully appreciated!
Well I figured out how to do this, and I guess I should put it into
the record, in particular because the answer is a bit non-obvious
(and even wierd).
I tried using Excel's Record New Macro... feature to see what
AutoFiltering a table looks like in VBA. The result was:
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 6/8/2006 by John Brock
'
'
Application.Goto Reference:="MyTableRange"
Selection.AutoFilter
End Sub
I had not seen Selection mentioned in any of the examples that were
sent to me, so I went looking for it in Visual Studio, but the only
place I found a Selection object was under the Application object.
It seemed strange to AutoFilter a range on a single worksheet by
doing something to the entire Application (which might have several
workbooks open), but it worked! Can anyone explain the logic to me?
In any case, the code looks like this:
Dim ws as Microsoft.Office.Interop.Excel.Worksheet
...
ws.Range("MyTableRange").Activate()
ws.Application.Selection.AutoFilter()
Also, does anybody have any idea what the AutoFilter function of
a Range object does (given that it doesn't turn AutoFiltering on)?
--
John Brock jb****@panix.com
In article <e6**********@reader2.panix.com>,
John Brock <jb****@panix.com> wrote:
[...] It seemed strange to AutoFilter a range on a single worksheet by doing something to the entire Application (which might have several workbooks open), but it worked! Can anyone explain the logic to me?
In any case, the code looks like this:
Dim ws as Microsoft.Office.Interop.Excel.Worksheet ...
ws.Range("MyTableRange").Activate() ws.Application.Selection.AutoFilter()
Also, does anybody have any idea what the AutoFilter function of a Range object does (given that it doesn't turn AutoFiltering on)?
Of course...
ws.Range("MyTableRange").Select()
ws.Application.Selection.AutoFilter()
also works, and using Select/Selection looks a bit more harmonious.
What exactly is the difference between Select and Activate anyway?
--
John Brock jb****@panix.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Brian Johnson |
last post by:
I am trying to create multiple (named) worksheets in excel
using .Net PIA's. I have successfully created an excel
document and set cell values, but have been unable to
manipulate which worksheet...
|
by: sridevi |
last post by:
Hello
How to export data from ms-access database to excel worksheet using ASP.
mainly i need to export data to multiple worksheets. it is very urgent to
us.
i have a sample code which works...
|
by: Otie |
last post by:
I found the following under the GetObject help notes and in the
example for GetObject:
"This example uses the GetObject function to get a reference to a
specific Microsoft Excel worksheet...
|
by: Mr. Smith |
last post by:
Hello all,
My code can successfully open, write to, format and save several
worksheets in a workbook then save it by a given name, close and quit excel.
My problem is that if I try and do it...
|
by: Horst Walter |
last post by:
I create an Excel worksheet in C# (should be similar in VB)
Connection String:
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename + ";" +
"Extended Properties=Excel 8.0;";
Create...
|
by: javzxp |
last post by:
Hi
I'd like to use C# to open an existing Excel workbook and save each
worksheet it contains into a new Excel file. The name of each new
Excel file should be the name of the worksheet copied...
|
by: Randall Arnold |
last post by:
I'm converting a vbscript program to vb.net. Witht he exception of .net
idiosyncrasies, most of it is working well with the same code. My only
problem is that some properties and methods are...
|
by: devolper |
last post by:
I am devolping a web application which which .net reports to a excel work book .each report in one excel sheet.i am using the following code..........
Dim objDestinationExcel As New...
|
by: TG |
last post by:
Hi!
I have an application in which I have some checkboxes and depending
which ones are checked those columns will show in the datagridview
from sql server or no.
After that I have 2 buttons:...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
header("Location:".$urlback);
Is this the right layout the...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: Ricardo de Mila |
last post by:
Dear people, good afternoon...
I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control.
Than I need to discover what...
| |