Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cut and Paste Columns in Excel Range with c#

Tags:

c#

excel

I'm trying to move column B in front of column Q in an excel sheet as part of a report I'm working on. I have experience in VBA but relatively little in c# so I've spent the last hour on Google and can't find a solution, I feel like this should be simple but I can't quite get it.

Method one, which results in a “Insert method of Range class failed” msg.

Excel.Range rngCut1 = JobLabourSheet.get_Range("B:B", Type.Missing);
Excel.Range rngPaste1 = JobLabourSheet.get_Range("Q:Q", Type.Missing);
            rngCut1.Columns.Cut(rngPaste1.EntireColumn.Insert(Excel.XlInsertShiftDirection.xlShiftToRight, rngCut1));

Method two results in a “Unable to get a Cut property of the Range class” msg.

Excel.Range rngCut1 = JobLabourSheet.get_Range("B:B", Type.Missing);
Excel.Range rngPaste1 = JobLabourSheet.get_Range("Q:Q", Type.Missing);
            rngCut1.Columns.Cut(rngPaste1.EntireColumn.Insert(Excel.XlInsertShiftDirection.xlShiftToRight, Missing.Value));

In the second method when I omit the CopyOrigin I get the msg but it does insert a blank column in front of column Q.

In VBA I would use the following:

Columns("B:B").Cut
Columns("Q:Q").Insert Shift:=xlToRight

But like I said, my c# experience is limited at the moment so I have no idea how to go about translating it to c#

like image 251
Cornelius Avatar asked Nov 08 '13 23:11

Cornelius


People also ask

How do you copy and paste a range of cells in Excel?

Select the cell or range of cells. Select Copy or press Ctrl + C. Select Paste or press Ctrl + V.

How do I cut and paste multiple columns in Excel?

After selecting the range of cells press Ctrl + C together to copy the range of cells. Again, select a range of cells where you want to paste it and press on to Ctrl + V together to paste it. This is the easiest way of copying and pasting multiple cells altogether.

How do I freeze columns AB and C in Excel?

Freeze columns and rows Select the cell below the rows and to the right of the columns you want to keep visible when you scroll. Select View > Freeze Panes > Freeze Panes.


1 Answers

This isn't very intuitive, but this is how I got it to work. I took an "insert" range and used the Insert() method and passed a "range.Cut()" method as the "Copy Origin" parameter.

Reference docs:

  • http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.range.insert.aspx
  • http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.xlinsertshiftdirection.aspx
  • http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.range.cut.aspx

Here is a sample app (be sure to add a reference to Microsoft.Office.Interop.Excel):

using System;
using System.Collections.Generic;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;

namespace ExcelCutAndInsertColumn
{
    class Program
    {
        static void Main(string[] args)
        {
            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWb = xlApp.Workbooks.Open(@"C:\stackoverflow.xlsx");
            Excel.Worksheet xlWs = (Excel.Worksheet)xlWb.Sheets[1]; // Sheet1

            xlApp.Visible = true;

            // cut column B and insert into A, shifting columns right
            Excel.Range copyRange = xlWs.Range["B:B"];
            Excel.Range insertRange = xlWs.Range["A:A"];

            insertRange.Insert(Excel.XlInsertShiftDirection.xlShiftToRight, copyRange.Cut());
        }
    }
}
like image 122
Joseph Avatar answered Sep 22 '22 13:09

Joseph