Insert New Column in Table Excel VBA

It is possible to add a column to a table in a particular place and name it, using the same line of code.

Table.ListColumns.Add(2).Name = "New Header"

This will add a column to the left of the second column in the table and name it New Header. You can make your code dynamic by adding a column to the left of one that you know the name of. This way, it is not necessary to specify the integer value of a new column's fixed location.

Dim newColNum as Integer
newColNum = Range("Table[Column Name]").Column
Table.ListColumns.Add(newColNum).Name = "New Header"

[Column Name] is the name of the column in your table where you want to insert a new column. It can have any position in the table, and you can pass it's value as an integer to the Add.


  Dim Table As ListObject
  Set Table = Sheet1.ListObjects("Table1")
  Table.ListColumns.Add 2
  Table.HeaderRowRange(2) = "New header"

Tags:

Excel

Vba