How to Delete Columns in VBA (With Examples)


You can use the following methods to delete columns in Excel using VBA:

Method 1: Delete One Column

Sub DeleteColumns()
Columns("C").Delete
End Sub

This particular example will delete column C from the current workbook.

Method 2: Delete All Columns in Range

Sub DeleteColumns()
Columns("B:D").Delete
End Sub

This particular example will delete all columns in the range B through D in the current workbook.

Method 3: Delete Several Specific Columns

Sub DeleteColumns()
Range("B:B, D:D").Delete
End Sub

This particular example will delete columns B and D in the current workbook.

The following examples show how to use each of these methods in practice with the following dataset in Excel:

Example 1: Delete One Column in VBA

We can create the following macro to delete only column C from our dataset:

Sub DeleteColumns()
Columns("C").Delete
End Sub

When we run this macro, we receive the following output:

Notice that only column C (the “Assists” column) has been deleted from the dataset.

Example 2: Delete All Columns in Range

We can create the following macro to delete all columns in the range from B to D:

Sub DeleteColumns()
Columns("B:D").Delete
End Sub

When we run this macro, we receive the following output:

Notice that each column in the range from B to D (the “Points”, “Assists”, and “Rebounds” columns) have been deleted from the dataset.

Example 3: Delete Several Specific Columns

We can create the following macro to delete columns B and D from the dataset:

Sub DeleteColumns()
Range("B:B, D:D").Delete
End Sub

When we run this macro, we receive the following output:

Notice that columns B and D (the “Points” and “Rebounds” columns) have been deleted from the dataset.

Additional Resources

The following tutorials explain how to perform other common tasks in VBA:

VBA: How to Count Number of Rows in Range
VBA: How to Count Number of Used Columns
VBA: How to Find Last Used Column

Leave a Reply

Your email address will not be published. Required fields are marked *