Insert a Column with a Name in Excel VBA – 5 Examples

 

Example 1 – Insert a Single Column with a Name in Excel Using VBA

The following dataset showcases sales data for different cities and in different dates. Use VBA to create a new column with the sales regions.

Insert Single Column with Name in Excel Using VBA

STEPS:

  • Right-click the active sheet (Single).
  • Select ‘View Code’.

Insert Single Column with Name in Excel Using VBA

  • A blank VBA code window for that worksheet will be displayed (you can also open the code window pressing Alt + F11).
  • Enter the 2 codes below in the code window:
Sub insert_column()
Range("C1").EntireColumn.insert
End Sub
Sub column_name()
Dim name: name = Split("Region")
Sheet1.Range("C4").Resize(1, UBound(name) + 1) = name
End Sub
  • Click Run or press F5 to run both codes.

Insert Single Column with Name in Excel Using VBA

  • This is the output.

The first code creates a new column and the second code names it.

Read More: Excel VBA: Cut and Insert Column


Example 2 – Use VBA to Add Multiple Columns with Names in Excel

To insert multiple columns with names:

Use VBA to Add Multiple Columns with Names in Excel

STEPS:

  • Right-click the active sheet (Multiple).
  • Select ‘View Code’.

Use VBA to Add Multiple Columns with Names in Excel

  • A blank VBA code window for that worksheet will be displayed (you can also open the code window pressing Alt + F11).
  • Enter the 2 codes below in the code window:
Sub insert_multiple_column()
Range("C:D").EntireColumn.insert
End Sub
Sub column_name()
Dim name: name = Split("Region,Area", ",")
Sheet2.Range("C4").Resize(1, UBound(name) + 1) = name
End Sub
  • Click Run or press F5 to run both codes.

Use VBA to Add Multiple Columns with Names in Excel

  • This is the output.

The first code inserts two columns and the second code names the columns.


Example 3 – Using VBA to Remove Formatting While Inserting a Column with a Name

To insert a column with a name without keeping the formatting:

VBA to Remove Formatting While Inserting Column with Name

STEPS:

  • Right-click the active sheet (Formatting).
  • Select ‘View Code’.

VBA to Remove Formatting While Inserting Column with Name

  • A blank VBA code window for that worksheet will be displayed (you can also open the code window pressing Alt + F11).
  • Enter the 2 codes below in the code window:
Sub formatting()
columns(3).EntireColumn.insert
columns(3).ClearFormats
End Sub
Sub column_name()
Dim name: name = Split("Region", ",")
Sheet3.Range("C4").Resize(1, UBound(name) + 1) = name
End Sub
  • Click Run or press F5 to run both codes.

VBA to Remove Formatting While Inserting Column with Name

  • The dataset has a new column., but it does not include formatting.

The first code creates a new column and the second code names the new column and removes its formatting.


Example 4 – Applying VBA to Add a Copied Column with a Name in Excel

Insert a copied column with the name:

Apply VBA to Add Copied Column with Name in Excel

STEPS:

  • Right-click the active sheet (Copy).
  • Select ‘View Code’.

Apply VBA to Add Copied Column with Name in Excel

  • A blank VBA code window for that worksheet will be displayed (you can also open the code window pressing Alt + F11).
  • Enter the code below in the code window:
Sub insert_column()
Application.CutCopyMode = False
With Worksheets("Copy")
.columns(3).Copy
.columns(5).insert Shift:=xlShiftDown
End With
Application.CutCopyMode = True
End Sub
  • Click Run or press F5 to run the code.

Apply VBA to Add Copied Column with Name in Excel

  • This is the output.


Example 5 – Insert a Column with a Name Based on Cell Value

Insert a new column (Region). It will be placed before the column that contains the cell value Sales.

Insert Column with Name Based on Cell Value

STEPS:

  • Right-click the active sheet (Cell Value).
  • Select ‘View Code’.

Insert Column with Name Based on Cell Value

  • A blank VBA code window for that worksheet will be displayed (you can also open the code window pressing Alt + F11).
  • Enter the 2 codes below in the code window:
Sub based_on_cell_value()
Dim k As Integer
Dim x As Integer
x = 2
For k = 2 To 12
If Cells(4, x).Value = "Sales" Then
Cells(4, x).EntireColumn.insert
x = x + 1
End If
x = x + 1
Next k
End Sub
Sub column_name()
Dim name: name = Split("Region")
Sheet5.Range("D4").Resize(1, UBound(name) + 1) = name
End Sub
  • Click Run or press F5 to run both codes.

Insert Column with Name Based on Cell Value

  • This is the output.

The first code inserts a new column before the column that contains a specific cell value. The second code names the column.


Download Practice Workbook

Download the practice workbook.

Get FREE Advanced Excel Exercises with Solutions!
Mukesh Dipto
Mukesh Dipto

Mukesh Dipto is an excellent marine engineer who loves working with Excel and diving into VBA programming. For him, programming is like a superhero tool that saves time when dealing with data, files, and the internet. His skills go beyond the basics, including Rhino3D, Maxsurf C++, AutoCAD, HTML, CSS, JavaScript, and WordPress. He got his B.Sc in Naval Architecture & Marine Engineering from BUET, and now he's switched gears, working as a content developer. In this role, he... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo