VBA Command Button: Insert TABLE Row

CokeOrCrack

Board Regular
Joined
Dec 13, 2015
Messages
81
I am attempting to use a Command Button to insert a new TABLE row. The code I have successfully inserts an entire row instead of a TABLE row. The code I have is as follows:

Private Sub CommandButton1_Click()
Dim mySheets
Dim i As Long

mySheets = Array("General")

For i = LBound(mySheets) To UBound(mySheets)
With Sheets(mySheets(i))
.Range("B12").EntireRow.Insert Shift:=xlDown
.Range("B12:O12").Borders.Weight = xlThin
End With
Next i
End Sub
--------------------------------------------------------------------------------------------
Notes:
The sheet name is: General
The table name is: General
--------------------------------------------------------------------------------------------
Is there a way to code a VBA Command Button so it inserts only a TABLE row upon clicking instead of an entire sheet row?

I assume my error has something to do with Line 9 where it states ".EntireRow.Insert" but I don't know what I would edit it to.

Thanks

OJ
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Depending on where you want to insert the row, there are two examples: Inserting a row above row 3 and inserting a row at the bottom.

Code:
Private Sub CommandButton1_Click()


    Dim mySheets
    Dim i As Long


    mySheets = Array("General")
    For i = LBound(mySheets) To UBound(mySheets)
        With Sheets(mySheets(i))
            Dim tbl As ListObject: Set tbl = ActiveSheet.ListObjects("General")
            tbl.ListRows.Add (3)   'Add row above row 3
            tbl.ListRows.Add alwaysinsert:=True  'Add row to bottom of table
            .Range("B12:O12").Borders.Weight = xlThin
        End With
    Next i
    
End Sub

I hope this helps
 
Upvote 0
igold

Thanks for the assistance. I'll give it a try in a bit when I'm near my work and let you know how it goes.

OJ
 
Upvote 0
Great, you're welcome. Thanks for the feedback!
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

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