Click here to Skip to main content
15,882,163 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Issue is my code always return one row instead of 13. I need to create a text file based on the filtered rows.But i am not getting the correct count and filtered rows.I will add and remove other clms .but row has issue.I need to get rows having colum value 01

C#
xlRange.AutoFilter(17, "01", Microsoft.Office.Interop.Excel.XlAutoFilterOperator.xlFilterValues, Type.Missing, true);

This line filtering but not giving correct counts of filtered row

What I have tried:

C#
public static void ExportToTextFile(Excel._Worksheet xlWorksheet, Excel.Workbook xlWorkbook, Excel.Application xlApp, string wbname)

        {

            //creating .txt file with new coloumns



            Excel._Worksheet xlWorksheetNew = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkbook.Sheets[1]; // excel sheet

            xlWorksheet.Copy(xlWorksheetNew); //copying from another xl

            Excel.Range excelRange = xlWorksheetNew.UsedRange;



            xlWorksheetNew.Cells[1, 18] = "FLAG";





            Microsoft.Office.Interop.Excel.Range xlRange = xlWorksheetNew.UsedRange;



          xlRange.AutoFilter(17,"01",Microsoft.Office.Interop.Excel.XlAutoFilterOperator.xlFilterValues, Type.Missing, true);


            int Columns = xlWorksheetNew.UsedRange.Columns.Count;

            int Rows = filteredRange.Rows.Count; //NOT GIVING CORRECT COUNT


            ((Excel.Range)xlWorksheetNew.Columns["P:Q"]).Delete();

            ((Excel.Range)xlWorksheetNew.Columns["C"]).Delete();

            ((Excel.Range)xlWorksheetNew.Columns["A:B"]).Delete();




            int AfterdeletColumns = xlWorksheetNew.UsedRange.Columns.Count;

            int afterdeletRows = filteredRange.Rows.Count;



             xlWorksheetNew.Visible = Excel.XlSheetVisibility.xlSheetHidden;


            ExportToTextFile(Rows, Columns, xlWorksheetNew);

        }
Posted
Updated 11-Dec-21 5:34am
v3
Comments
Richard MacCutchan 11-Dec-21 7:08am    
" //NOT GIVING CORRECT COUNT"
What count is returned and why is it not correct?
[no name] 11-Dec-21 7:13am    
I need row count after filtering some rows.I cross checked the count of filterd data but in code always give 1 only
Richard Deeming 13-Dec-21 4:18am    
There are only two references to your filteredRange variable, and both are trying to read something from it. You haven't declared or initialized it anywhere.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900