Compartir a través de


ControlCollection.AddNamedRange(Range, String) Method

Definition

Adds a new NamedRange control to the worksheet at the range specified.

public:
 Microsoft::Office::Tools::Excel::NamedRange ^ AddNamedRange(Microsoft::Office::Interop::Excel::Range ^ range, System::String ^ name);
public Microsoft.Office.Tools.Excel.NamedRange AddNamedRange (Microsoft.Office.Interop.Excel.Range range, string name);
abstract member AddNamedRange : Microsoft.Office.Interop.Excel.Range * string -> Microsoft.Office.Tools.Excel.NamedRange
Public Function AddNamedRange (range As Range, name As String) As NamedRange

Parameters

range
Range

A Range that provides the bounds for the control.

name
String

The name of the control that can be used to index the control in the ControlCollection instance.

Returns

The NamedRange control that was added to the ControlCollection instance.

Exceptions

The name or range argument is null, or the name argument has zero length.

A control with the same name is already in the ControlCollection instance.

The range that was specified is not valid. Multi-area ranges cannot be used. The range should be on the same worksheet as the ControlCollection instance.

Examples

The following code example adds a NamedRange control to cell A1, and then sets the value of the named range to 100%.

private void ExcelAddNamedRange()
{
    Microsoft.Office.Tools.Excel.NamedRange namedRange1 =
        this.Controls.AddNamedRange(this.Range["A1"],
        "namedRange1");
    namedRange1.Value2 = "100%";
}
Private Sub ExcelAddNamedRange()

    Dim NamedRange1 As Microsoft.Office.Tools. _
      Excel.NamedRange = Me.Controls.AddNamedRange( _
      Me.Range("A1"), "NamedRange1")
    NamedRange1.Value2 = "100%"

End Sub

Remarks

The AddNamedRange method enables you to add NamedRange controls to the end of the ControlCollection. To remove a NamedRange control that was previously added programmatically, use the Remove method.

Applies to