Applies To
Application Object, Range Object, Worksheet Object.
Description
Accessor. Returns a Range object that represents a single column (Syntax 1) or a collection of columns (Syntax 2).
Syntax 1
object.Columns(index)
Syntax 2
object.Columns
object
Optional for Application, required for Range and Worksheet. The object that contains the columns. If you specify the Application object (or omit the object qualifier), this method applies to the active sheet in the active workbook. If the active sheet is not a worksheet, this method fails.
index
Required for Syntax 1. The name or number of the column.
Remarks
When applied to a Range object that is a multiple selection, this method returns columns from the first area of the range only. For example, if the Range object is a multiple selection with two areas, A1:B2 and C3:D4, Selection.Columns.Count returns 2, not 4. To use this method on a range that may contain a multiple selection, test Areas.Count to determine if the range is a multiple selection, and if it is, then loop over each area in the range; see the third example.
See Also
Range Method, Rows Method.
Example
This example formats the font of column one (column A) on Sheet1 as bold.
Worksheets("Sheet1").Columns(1).Font.Bold = True
This example sets the value of every cell in column one in the range named myRange to zero (0).
Range("myRange").Columns(1).Value = 0
This example displays the number of columns in the selection on Sheet1. If more than one area is selected, the example loops through each area.
Worksheets("Sheet1").Activate areaCount = Selection.Areas.Count If areaCount <= 1 Then MsgBox "The selection contains " & _ Selection.Columns.Count & " columns." Else For i = 1 To areaCount MsgBox "Area " & i & " of the selection contains " & _ Selection.Areas(i).Columns.Count & " columns." Next i End If