473,387 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Converting VBA to C# for Excel AutoFilter function

Hello all, I've got some code I wrote using VBA for Excel that I'm trying to recreate using C#.NET. The code autofilters 1 entire column from 1 Worksheet from a Workbook and then appends this filtered code to a new column in a new Worksheet in a new Workbook.

First, the VBA code:
Expand|Select|Wrap|Line Numbers
  1. Set rData = wsData.Range("BQ5", wsData.Range("BQ65536").End(xlUp))
  2. Set rTargetCl = wsTarget.Range("B65536").End(xlUp).Offset(1,0)
  3. With rData
  4.     .AutoFilter Field:=1, Criteria:=">0", Operator:=xlAnd
  5.     rData.Offset(1,0).SpecialCells(xlCellTypeVisible).Copy Destination:=rTargetCl
  6.     .AutoFilter
  7. End With
  8.  
I've tried this in C# but am getting a Cast exeption on the AutoFilter line:
Expand|Select|Wrap|Line Numbers
  1. rData = (Excel.Range)wsData.get_Range("BQ5", wsData.get_Range("BQ65536", Type.Missing).get_End(Excel.xlDirection.xlUp));
  2. rTargetCl = (Excel.Range)wsTarget.get_Range("B65536", Type.Missing).get_End(Excel.xlDirection.xlUp).get_Offset(1,0);
  3.  
  4. // I get the error on this line
  5. ((Excel.Range)rData.AutoFilter(1,">0",Excel.xlAutoFilterOperator.xlAnd, Type.Missing, Type.Missing)).get_Offset(1,0).SpecialCells(Excel.xlCellType.xlCellTypeVisible, Type.Missing).Copy(rTargetCl);
  6.  
I cannot remove the Range cast because if I do I don't get any other options after the final AutoFilter paren. And yet that cast appears to be causing the problem.

Any help would be greatly appreciated.
Oct 20 '06 #1
1 8173
I figured it out. I just needed to break it up into two lines.

Like this:
Expand|Select|Wrap|Line Numbers
  1. rData.Autofilter(1, ">0", Excel.XlAutoFilterOperator.xlAnd, Type.Missing, Type.Missing);
  2. rData.get_Offset(1,0).SpecialCells(Excel.XlCellType.xlCellTypeVisible, Type.Missing).Copy(rTargetCl);
  3.  
Voila!
Oct 20 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: CCC | last post by:
Our company have used Excel to filter out documents base on date, from_company, type_of_document, country to generate a list of where the documents physical location is. The number of records have...
2
by: JM | last post by:
Anyone know how to do an autofilter on a workbook in C#? Any snippets of code or reference material would be helpful!
0
by: Jon B | last post by:
Hi Everyone! I'm in need of help again! I'm just wondering if WinForm DataGrid can have Excel like AutoFilter? Here's an example: http://www.dotnetforce.com/Content.aspx?t=a&n=210 But again,...
9
by: John Brock | last post by:
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")...
0
by: denvercr | last post by:
Hi Guys, I need to embed a dropdown list in my excel automation in C#, Do you have any idea how to use Excel.Range.AutoFilter method? or any suggested sites where I can learn this thing?.. Thank...
4
by: rnot | last post by:
Hello, I would like to paste data at a cell found by the Selection.Autofilter function. My two first columns hold a "scenario" and "replication" data. I would like upon running a macro to...
10
by: afromanam | last post by:
Regards, Please help What I'm trying to do is this: (and I can't use reports since I must export to Excel) I export some queries to different tabs in an excel workbook I then loop through...
1
by: ericjohnson | last post by:
I've got a macro that has already been written for an Excel spreadsheet used for estimating at work. I know enough VB to write very basic macros, but I cannot understand the logic and purpose behind...
1
by: bluejeanbaby | last post by:
Hi I'm trying to format an Excel file from within VB.NET 2008 (Framework 2.0). I'm not very familiar with Excel but have taken existing working code from an old VB6 program and am "tweaking" it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.