How to Copy Cells

by Chaknith Bin

The "Copy cell" feature allows you to duplicate the contents of a cell and paste them into one or more other cells. It is a convenient way to replicate data, formulas, formatting, or other attributes within the worksheet.

Additionally, the Copy method also retains styling, enabling efficient and accurate data replication within a single or multiple worksheets using IronXL.


C# NuGet Library for Excel

Install with NuGet

Install-Package IronXL.Excel
or
C# Excel DLL

Download DLL

Download DLL

Manually install into your project

Copy a Single Cell Example

To copy the content of a selected cell, you can use the Copy method. Pass the worksheet object as the first parameter and the starting position as the second parameter. The Copy method also retains any styling that the cell contains.

:path=/static-assets/excel/content-code-examples/how-to/copy-cells-copy-single-cell.cs
using IronXL;

WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");

// Copy cell content
workSheet["A1"].Copy(workBook.GetWorkSheet("Sheet1"), "B3");

workBook.SaveAs("copySingleCell.xlsx");
Imports IronXL

Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.GetWorkSheet("Sheet1")

' Copy cell content
workSheet("A1").Copy(workBook.GetWorkSheet("Sheet1"), "B3")

workBook.SaveAs("copySingleCell.xlsx")
VB   C#

Output Spreadsheet

Copy Single Cell

Copy Cell Range Example

Similar to the Clear method, this method is also available in the Range class, allowing you to execute it on any range, regardless of its size. Here are some examples:

Copy a single cell (C10):

  • workSheet["C10"].Copy(workBook.GetWorkSheet("Sheet1"), "B13")

Copy a column (A):

  • workSheet.GetColumn(0).Copy(workBook.GetWorkSheet("Sheet1"), "H1")

Copy a row (4):

  • workSheet.GetRow(3).Copy(workBook.GetWorkSheet("Sheet1"), "A15")

Copy a two-dimensional range (D6:F8):

  • workSheet["D6:F8"].Copy(workBook.GetWorkSheet("Sheet1"), "H17")

Please note
The second parameter accepts an address location that marks the starting point of data entry. The copied data will start from that address and spread rightward and downward.

:path=/static-assets/excel/content-code-examples/how-to/copy-cells-copy-cell-range.cs
using IronXL;

WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");

// Copy a single cell(C10)
workSheet["C10"].Copy(workBook.GetWorkSheet("Sheet1"), "B13");

// Copy a column(A)
workSheet.GetColumn(0).Copy(workBook.GetWorkSheet("Sheet1"), "H1");

// Copy a row(4)
workSheet.GetRow(3).Copy(workBook.GetWorkSheet("Sheet1"), "A15");

// Copy a two-dimensional range(D6:F8)
workSheet["D6:F8"].Copy(workBook.GetWorkSheet("Sheet1"), "H17");

workBook.SaveAs("copyCellRange.xlsx");
Imports IronXL

Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.GetWorkSheet("Sheet1")

' Copy a single cell(C10)
workSheet("C10").Copy(workBook.GetWorkSheet("Sheet1"), "B13")

' Copy a column(A)
workSheet.GetColumn(0).Copy(workBook.GetWorkSheet("Sheet1"), "H1")

' Copy a row(4)
workSheet.GetRow(3).Copy(workBook.GetWorkSheet("Sheet1"), "A15")

' Copy a two-dimensional range(D6:F8)
workSheet("D6:F8").Copy(workBook.GetWorkSheet("Sheet1"), "H17")

workBook.SaveAs("copyCellRange.xlsx")
VB   C#

Output Spreadsheet

Copy Cell Range

Copy Cell Across Worksheet Example

Since the first parameter accepts a worksheet object, it is possible to copy and paste a cell range across different worksheets. Simply pass a different worksheet object as the first parameter.

Please note
In the following example, the first parameter of the Copy method is the "Sheet2" worksheet: workBook.GetWorksheet("Sheet2")

:path=/static-assets/excel/content-code-examples/how-to/copy-cells-copy-to-other-worksheet.cs
using IronXL;

WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");

// Copy cell content
workSheet["A1"].Copy(workBook.GetWorkSheet("Sheet2"), "B3");

workBook.SaveAs("copyAcrossWorksheet.xlsx");
Imports IronXL

Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.GetWorkSheet("Sheet1")

' Copy cell content
workSheet("A1").Copy(workBook.GetWorkSheet("Sheet2"), "B3")

workBook.SaveAs("copyAcrossWorksheet.xlsx")
VB   C#

Chaknith Bin

Software Engineer

Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.