Answered by:
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
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);
All replies



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);


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/enUS/csharpgeneral/thread/920180bf1c8440f7
b547ba9532e309cd
Thanks
Shyam N S Proposed as answer by Extreme Spartan Thursday, March 18, 2010 6:25 AM
