Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Delete a Column Programmatically?

How does one delete a column (or multiple columns) in Excel?

eg. How to delete column C and shift the rest left?

like image 989
Aximili Avatar asked Aug 19 '10 00:08

Aximili


1 Answers

This was the first result I hit and deleting a column in Excel doesn't need as much code as the current answers suggest. In fact (assuming you have a Worksheet object already, listed below as mySheet) all that is needed for the original question is:

mySheet.Columns["C"].Delete();

If you want to delete multiple columns then:

mySheet.Columns["C:D"].Delete();

You can specify a variable in the Delete method (see https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel.xldeleteshiftdirection?view=excel-pia) i.e. mySheet.Columns["C"].Delete(xlShiftToLeft)but there's no need as the Delete method is smart enough to realise that the Range you are selecting is a single column, so will do this automatically.

You can also uses a numeric value to designate the column i.e. mySheet.Columns[2].Delete()

like image 53
d219 Avatar answered Oct 20 '22 20:10

d219