none
How to select and delete two or more rows in Excel (C#) (without loop)

    Question

  • Maybe somebode know how can I delete several rows in Excel .

     

    I know how can I delete one row (C#)

     

    Excel.Range ran;

    ran = (Excel.Range)this.Application.Rows[12, missing];

    ran.Select();

    ran.Delete(Excel.XlDirection.xlUp);

     

     

    I don't know how I can make that Range will be several rows ,for example 12,13

     

    Thanks

    smugliy

    Friday, February 15, 2008 1:21 PM

Answers

  •  smugliy77 wrote:

    maybe you also know ,how can I replace "a12" with some variable ?

     

    so I can do it from code programmaticly ?

    You'll notice that the cell references are strings. Since the column number is immaterial, you can combine the integer you need with any letter (column reference) then use these strings:

     

    int row1 = 12;

    int row2 = 14;

    string cell1 = "A" + row1.ToString();

    string cell2 = "A" + row2.ToString();

    //Excel.Range rng = ws.get_Range("a12", "A14");

    Excel.Range rng = ws.get_Range(cell1, cell2);

    rng.EntireRow.Delete(Excel.XlDirection.xlUp);

    Friday, February 15, 2008 5:12 PM
    Moderator

All replies

  • Here's one possibility

     

    //Get a set of cells in the rows

    Excel.Range rng = ws.get_Range("a12", "A14");

    //Then act on the entire rows of the range

    rng.EntireRow.Delete(Excel.XlDirection.xlUp);

     

    Friday, February 15, 2008 2:06 PM
    Moderator
  •  

    Thanks , Cindy ,

     

    maybe you also know ,how can I replace "a12" with some variable ?

     

    so I can do it from code programmaticly ?

    Friday, February 15, 2008 4:44 PM
  •  smugliy77 wrote:

    maybe you also know ,how can I replace "a12" with some variable ?

     

    so I can do it from code programmaticly ?

    You'll notice that the cell references are strings. Since the column number is immaterial, you can combine the integer you need with any letter (column reference) then use these strings:

     

    int row1 = 12;

    int row2 = 14;

    string cell1 = "A" + row1.ToString();

    string cell2 = "A" + row2.ToString();

    //Excel.Range rng = ws.get_Range("a12", "A14");

    Excel.Range rng = ws.get_Range(cell1, cell2);

    rng.EntireRow.Delete(Excel.XlDirection.xlUp);

    Friday, February 15, 2008 5:12 PM
    Moderator
  • Thanks a lot

     

    Saturday, February 16, 2008 8:27 AM
  • Hi ,

    To delete several rows in excel use the range property :


    Excel.Range range = clsWorksheet.get_Range("A3","A20".ToString());
    //setting the range for deleting the rows

    range.EntireRow.Delete(Excel.XlDirection.xlUp);
    //for deleting the rows; in case of columns use: 'EntireColumn' instead of 'EntireRow'



    for more details on how to delete from the scratch, see :

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/920180bf-1c84-40f7-
    b547-ba9532e309cd


    Thanks
    Shyam N S


    Thursday, March 18, 2010 6:25 AM
  • How might the code be different using VSTO 2010, C# targeting the .NET Framework 4.0 for client?
    Sunday, March 27, 2011 10:18 PM