Share via


ControlCollection.AddListObject Method (, String) (2007 System)

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

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

Syntax

'Declaration
Public Function AddListObject ( _
    range As Range, _
    name As String _
) As ListObject
'Usage
Dim instance As ControlCollection 
Dim range As Range 
Dim name As String 
Dim returnValue As ListObject 

returnValue = instance.AddListObject(range, _
    name)
public ListObject AddListObject(
    Range range,
    string name
)
public:
ListObject^ AddListObject(
    Range^ range, 
    String^ name
)
public function AddListObject(
    range : Range, 
    name : String
) : ListObject

Parameters

  • range
    Type: Range

    A Range that provides the bounds for the control.

Return Value

Type: Microsoft.Office.Tools.Excel.ListObject
The ListObject control that was added to the ControlCollection instance.

Exceptions

Exception Condition
ArgumentNullException

The name or range argument is nulla null reference (Nothing in Visual Basic), or the name argument has zero length.

ControlNameAlreadyExistsException

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

InvalidRangeException

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.

Remarks

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

Examples

The following code example adds a ListObject control to the top of the worksheet, and then displays a message box showing the number of rows in the ListObject.

Private Sub ExcelAddListObject()
    Dim List1 As Microsoft.Office.Tools.Excel. _
        ListObject = Me.Controls.AddListObject( _
        Me.Range("A1", "C5"), "List1")

    MessageBox.Show("The list object has " _
        & List1.Range.Rows.Count.ToString() + " rows.")

End Sub
private void ExcelAddListObject()
{
    Microsoft.Office.Tools.Excel.ListObject list1 =
        this.Controls.AddListObject(this.Range["A1", "C5"], 
        "list1");
    MessageBox.Show("The list object has " + 
        list1.Range.Rows.Count.ToString() + " rows.");
}

.NET Framework Security

See Also

Reference

ControlCollection Class

ControlCollection Members

AddListObject Overload

Microsoft.Office.Tools.Excel Namespace