467,074 Members | 1,087 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,074 developers. It's quick & easy.

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

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
Nov 16 '05 #1
  • viewed: 34522
Share:
2 Replies
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.
"Mansi" <Ma***@discussions.microsoft.com> wrote in message
news:3C**********************************@microsof t.com...
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

Nov 16 '05 #2
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

"Bonj" wrote:
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.
"Mansi" <Ma***@discussions.microsoft.com> wrote in message
news:3C**********************************@microsof t.com...
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


Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Faith | last post: by
7 posts views Thread by lgbjr | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.