Professional Documents
Culture Documents
When this code runs, Microsoft Excel must be displaying the second worksheet. If you run this code
while Microsoft Excel is displaying a worksheet other than the second, you would receive an error:
If you want to access a specific column in any worksheet from the workbook, you can omit
indicating the worksheet from the Worksheets collection. Here is an example:
Sub Exercise()
www.functionx.com/vbaexcel/topics/columns.htm
1/5
24/07/13
VBA For Microsoft Excel: The Columns of a Spreadsheet
Sub Exercise()
REM This refers to the fourth column
Columns(4)
End Sub
This time, the code indicates that you are referring to the fourth column of whatever worksheet is
currently active.
Adjacent Columns
To programmatically refer to adjacent columns, you can use the Columns collection. In its
parentheses, type the name of a column that would be on one end of the range, followed by a
colon ":", followed by the name of the column that would on the other end. Here is an example that
refers to columns in the range D to G:
Sub ColumnReference()
Rem Refer to the range of columns D to G
Columns("D:G")
End Sub
You can also select columns using the Range class. To do this, type the name of the first column,
followed by a colon, followed by the name of the column on the other end. Here is an example:
Sub ColumnReference()
Rem This refers to the columns in the range D to H
Range("D:H")
End Sub
Non-Adjacent Columns
To programmatically refer to non-adjacent columns, use the Range collection. In its parentheses,
type each name of a column, followed by a colon, followed by the same name of column, then
separate these combinations with commas. Here is an example:
Sub Exercise()
Rem This refers to Columns H, D, and B
Range("H:H, D:D, B:B")
End Sub
To refer to all columns of a worksheet, use the Columns name. Here is an example:
Sub Exercise()
Columns
End Sub
Columns Selection
Selecting a Column
To support column selection, the Column class is equipped with a method named Select. This
method does not take any argument. To select the fourth column using its index, you would use
code as follows:
Sub Exercise()
Rem This selects the fourth column
Columns(4).Select
End Sub
To select a column using its name, you would use code as follows:
Sub Exercise()
Rem This selects the column labeled ADH
Columns("ADH").Select
End Sub
When a column has been selected, it is stored in an object called Selection. You can then use
that object to take an action to apply to the column.
www.functionx.com/vbaexcel/topics/columns.htm
2/5
24/07/13
Creating Columns
Adding a New Column
To support the creation of columns, the Column class is equipped with a method named Insert.
This method takes no argument. When calling it, you must specify the column that will succeed the
new one. Here is an example that will create a new column in the third position and move the
columns from 3 to 16384 to the right:
Sub CreateColumn()
Columns(3).Insert
End Sub
Deleting Columns
Deleting a Column
To provide the ability to delete a column, the Column class is equipped with a method named
Delete. This method does not take an argument. To delete a column, use the Columns collection
to specify the index or the name of the column that will be deleted. Then call the Delete method.
Here is an example that removes the fourth column. Here is an example:
Sub DeleteColumn()
Columns("D:D").Delete
End Sub
www.functionx.com/vbaexcel/topics/columns.htm
3/5
24/07/13
4/5
24/07/13
Home
www.functionx.com/vbaexcel/topics/columns.htm
5/5