Share via


NamedRange.Columns Property

Gets a Microsoft.Office.Interop.Excel.Range that represents the one or more columns in the NamedRange control.

Namespace:  Microsoft.Office.Tools.Excel
Assembly:  Microsoft.Office.Tools.Excel (in Microsoft.Office.Tools.Excel.dll)

Syntax

'Declaration
ReadOnly Property Columns As Range
    Get
Range Columns { get; }

Property Value

Type: Microsoft.Office.Interop.Excel.Range
A Microsoft.Office.Interop.Excel.Range that represents one or more columns in the NamedRange control.

Remarks

When used without parameters, this property returns a Range object that contains all the columns in the named range.

This property can be used with the following optional parameters to get specific columns in the named range. If you use this property with parameters, the return value is an object that must be cast to a Range.

Parameter

Description

RowIndex

The index of one or more columns to get.

To get a single column, pass one of the following objects to this parameter:

  • An integer that specifies the index of the column you want to get. The column indexes begin at 1.

  • A string that consists of the letter of the column you want to get.

To get multiple contiguous columns, pass a string with the format "first column letter:last column letter". For example, to get columns A through E, pass "A:E".

NoteNote
The name of this parameter is misleading; this parameter specifies the indexes of the columns you want to get, not the rows.

ColumnIndex

Do not use this parameter. This property will throw a COMException if you try to pass a value to this parameter.

Examples

The following code example creates a NamedRange and then uses the Columns property to set the color, name, size, and boldness of the font of all the cells in the first column of the range.

This example is for a document-level customization. To run this code, copy it into one of the worksheet classes in your project.

Private Sub SetFirstColumnFont()
    Dim testRange As Microsoft.Office.Tools.Excel.NamedRange = _
        Me.Controls.AddNamedRange(Me.Range("A1", "J10"), _
        "TestRange")
    testRange.Select()

    Dim fillColumn As Excel.Range = TryCast(testRange.Columns("A"), Excel.Range)
    With fillColumn.Font
        ' Set the font color to blue (RGB value 00 00 FF), and set other font properties.
        .Color = &HFF0000
        .Name = "Arial"
        .Size = 14
        .Bold = False
    End With

    ' Test the changes by writing a value to all the column cells.
    fillColumn.Value2 = "This is a test"
End Sub
private void SetFirstColumnFont()
{
    Microsoft.Office.Tools.Excel.NamedRange testRange = 
        this.Controls.AddNamedRange(this.Range["A1", "J10"],
        "TestRange");
    testRange.Select();

    Excel.Range fillColumn = (Excel.Range)testRange.Columns["A", missing];
    Excel.Font columnsFont = fillColumn.Font;

    // Set the font color to blue (RGB value 00 00 FF), and set other font properties.
    columnsFont.Color = 0xFF0000;
    columnsFont.Name = "Arial";
    columnsFont.Size = 14;
    columnsFont.Bold = false;

    // Test the changes by writing a value to all the column cells.
    fillColumn.Value2 = "This is a test";
}

The following code example creates a NamedRange and then uses the Columns property to determine how many columns there are in the range.

This example is for a document-level customization. To run this code, copy it into one of the worksheet classes in your project.

Private compositeRange As Microsoft.Office.Tools.Excel.NamedRange

Private Sub DisplayRangeComposition()
    compositeRange = Me.Controls.AddNamedRange( _
        Me.Range("B2", "E5"), "compositeRange")
    compositeRange.Cells.Interior.Color = &HFF00
    MessageBox.Show("The range has " & _
        compositeRange.Count & " cells.")
    MessageBox.Show("The range has " & _
        compositeRange.Columns.Count & " columns.")
    MessageBox.Show("The range has " & _
        compositeRange.Rows.Count & " rows.")
End Sub
Microsoft.Office.Tools.Excel.NamedRange compositeRange;
private void DisplayRangeComposition()
{
    compositeRange = this.Controls.AddNamedRange(
        this.Range["B2", "E5"], "compositeRange");
    compositeRange.Cells.Interior.Color = 0xFF00;
    MessageBox.Show("The range has " + compositeRange.Count + 
        " cells.");
    MessageBox.Show("The range has " + 
        compositeRange.Columns.Count + " columns.");
    MessageBox.Show("The range has " + 
        compositeRange.Rows.Count + " rows.");
}

.NET Framework Security

See Also

Reference

NamedRange Interface

Microsoft.Office.Tools.Excel Namespace