Exporting to Excel - how to copy/paste/insert rows?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm trying to automate excel from c#. One of the things I need to do is to
copy/paste/insert rows in excel via c# code.

I tried to do the following:
1) Select a row in excel (a12 to k12)
2) Insert a row.
3) copy contents of a12 to k12.
4) Paste these contents to new row.

The "Record Macro" function returns the following code:
Range("A13:K13).Select
Selection.Insert shift:=xlDown
Range("A14:k14).Select
Selection.Copy
Range("A13:k13").Select
ActiveSheet.Paste

I tried to translate part of the code above to the following:

oRange = oSheet.get_Range("A13", "K13");
oRange.Select();
oExcel.ActiveCell.Insert(-4121);

I needed the code to insert a new row in the specified range. But instead,
it just inserts a row for cell A13.

What am i doing wrong?

Mansi
 
Try
const int xlShiftDown = -4121
Excel.Range oRange = oSheet.get_Range("A13");
Excel.Range oRow = oRange.EntireRow;
oRow.Insert(xlShiftDown);

don't bother with 'Select' and 'Selection'. You should eliminate them from
macro-recorded code.
 
Thanks... The insert works great!

Is there a way to copy any formatting/formulas that previously existed in
the row for "A13" to the newly inserted row?

Thanks.
Mansi
 
Back
Top