473,486 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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
2 35196
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
32888
by: Faith | last post by:
Hello. I need to take a column from Excel(unknown amount of rows) that will be selected by the user and copy those cells. Then I will need to paste those cells into the first column in a Data...
7
11596
by: lgbjr | last post by:
Hello All, I¡¯m using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard was easy. But pasting an image from the clipboard...
0
1607
by: Richard Crowley | last post by:
I have a dynamically-generated asp web page which uses Office Web Components to display an Excel bar chart of the user-selected data. The page works fine, but the users would like to copy-n-paste...
0
2316
by: Nicholas Dreyer | last post by:
Operating System: Microsoft Windows Version 5.1 (Build 2600.xpsp_sp2_gdr.050301-1519 : Service Pack 2) Visual Basic: MIcrosoft Visual Basic 6.3 Version 9972 VBA: Retail 6.4.9972 Forms3:...
0
1501
by: six888 | last post by:
i need to make a report using excel. im given a few data in excel document for a 1 month report (1 document for each week). each data document have a few work sheet.i need only certain information in...
1
2310
by: StanOlli | last post by:
Hi, Im trying to copy 4 rows from an Excel sheet and paste it in the same sheet ( in C#). e.g. copy rows 8-11 and paste it to row 20. (20-23) Code snippets are appreciate. Thanks Stan
2
2764
by: OfficeDummy | last post by:
Hi, everyone! Like I mentioned in the thread title, I need to copy&paste data between different workbooks, and it works fine. However, when the data has been copied to the destination workbook,...
8
15388
by: jh | last post by:
I'd like to copy/paste into a listbox during runtime. I can do this for a textbox but can't figure out how to accomplish this for a listbox. Any help? Thanks.
0
1770
by: premMS143 | last post by:
I've created a application using VB. Here, how to code to insert number of rows (user mentioned) in an existing excel worksheet ....? In this, User has prompted, 1. Select an existing excel file...
0
7094
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6839
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7305
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4863
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3066
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.