Add a new row to a table called "Table1"

luolovepi

Board Regular
Joined
Jun 9, 2011
Messages
116
I would like to add in one more row as the last row of the exsiting table. "tableContentRows + 14" is the row number in the worksheet. Is the following correct?

Code:
Range("Table1").ListObject.ListRows.Add (tableContentRows + 14)
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Code:
[COLOR="Blue"]Sub[/COLOR] AddToTable()
    [COLOR="Blue"]Dim[/COLOR] i [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Integer[/COLOR]
    [COLOR="Blue"]With[/COLOR] Range("Table1").ListObject
        [COLOR="Blue"]For[/COLOR] i = 1 [COLOR="Blue"]To[/COLOR] 14
            .ListRows.Add Position:=.ListRows.Count + 1
        [COLOR="Blue"]Next[/COLOR]
    [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]With[/COLOR]
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Upvote 0
Hi Sektor,
Your code add rows at the end of the table. How about I only want to insert the new rows below above the last 3rd row of the table?

And could you pls teach me how listobject and listrows are used? I actually use the code below to select part of the table.
In my code, table name "Table1" is an argument of ListObjects, and ListObjects seems to be a property of worksheet.
In your code, ListObjects becomes a property of a range?! And is Range("Table1") the same as AcitveSheet.ListObjects("Table1")?
Rich (BB code):
Sub SelectingPartOfTable()
    Dim oSh As Worksheet
    Set oSh = ActiveSheet
    '1: with the listobject
    With oSh.ListObjects("Table1")
        'Select entire table
        .Range.Select
        'Select just the data of the entire table
        .DataBodyRange.Select
        'Select third column
        .ListColumns(3).Range.Select
    End With
End Sub




Code:
[COLOR=blue]Sub[/COLOR] AddToTable()
    [COLOR=blue]Dim[/COLOR] i [COLOR=blue]As[/COLOR] [COLOR=blue]Integer[/COLOR]
    [COLOR=blue]With[/COLOR] Range("Table1").ListObject
        [COLOR=blue]For[/COLOR] i = 1 [COLOR=blue]To[/COLOR] 14
            .ListRows.Add Position:=.ListRows.Count + 1
        [COLOR=blue]Next[/COLOR]
    [COLOR=blue]End[/COLOR] [COLOR=blue]With[/COLOR]
[COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR]
 
Upvote 0
And do you know how to delete a row from the table? For example, delete the blank rows together with their corresponding buttons which are located outside the table.
Note: the buttons are named after rowNumber.

Thank you in advance~

Best regards,
lolo

Code:
[COLOR=blue]Sub[/COLOR] AddToTable()
    [COLOR=blue]Dim[/COLOR] i [COLOR=blue]As[/COLOR] [COLOR=blue]Integer[/COLOR]
    [COLOR=blue]With[/COLOR] Range("Table1").ListObject
        [COLOR=blue]For[/COLOR] i = 1 [COLOR=blue]To[/COLOR] 14
            .ListRows.Add Position:=.ListRows.Count + 1
        [COLOR=blue]Next[/COLOR]
    [COLOR=blue]End[/COLOR] [COLOR=blue]With[/COLOR]
[COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,909
Members
452,949
Latest member
beartooth91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top