Excel VBA: Deleting the First Column

This Excel VBA macro deletes the first column of the first table on an active worksheet.

Macro Example

Sub DeletingFirstColumn()

  MsgBox ActiveSheet.ListObjects(1).ListColumns(1).Delete

End Sub

Description

[Application.]ActiveSheet

  ActiveSheet...

The [Application.]ActiveSheet property returns an active Worksheet or Chart object. So the Object Browser shows us that returned value is “Object”.

obj-Excel-Application-ActiveSheet

The ActiveSheet property is a member of the Excel.Global class, so we can omit the Application property.

obj-Excel-Global-ActiveSheet

ActiveSheet.ListObjects (Worksheet.ListObjects)

  ActiveSheet.ListObjects...

The ActiveSheet.ListObjects (Worksheet.ListObjects property) returns a ListObjects collection object on the active worksheet.

obj-Excel-Worksheet-ListObjects

ListObjects(1) (ListObjects._Default(1) )

  ActiveSheet.ListObjects(1)...

The ListObjects._Default property returns a single ListObject object from the ListObjects collection object.

obj-Excel-ListObjects-_Default

And the ListsObjects._Default(1) represents the first ListObject object.

The _Default property is a default member of the ListObjects collection object. We can see a little blue marble beside its icon in the Object Browser.
So we can omit the ._Default .

ListObjects(1).ListColumns (ListObject.ListColumns )

  ActiveSheet.ListObjects(1).ListColumns...

The ListObjects(1).ListColumns (ListObject.ListColumns property) returns a ListColumns collection object that represents all the columns in the ListObject object.

obj-Excel-ListObject-ListColumns

LinstColumns(1) (ListColumns._Default(1) )

  ActiveSheet.ListObjects(1).LinstColumns(1)...

The ListColumns._Default property returns a single ListColumn object from the ListColumns collection object.

obj-Excel-ListColumns-_Default

And the ListColumns._Default(1) represents the first ListColumn object.

The _Default property is a default member of the Columns collection object. We can see a little blue marble beside its icon in the Object Browser.
So we can omit the ._Default .

ListColumns(1).Delete (ListColumn.Delete)

The ListColumns(1).Delete (ListColumn.Delete method) deletes the column.

obj-Excel-ListColumn-Delete

Sub DeletingFirstColumn()

  MsgBox ActiveSheet.ListObjects(1).ListColumns(1).Delete

End Sub

Properties

http://www.relief.jp/itnote/archives/excel-macro-delete-column-in-table.php

Apply to

  • Excel 2013
  • Excel 2010
  • Excel 2007