VBA code need to run twice before adding a new row to table

bumbum2812

New Member
Joined
Sep 7, 2020
Messages
25
Office Version
  1. 365
Platform
  1. Windows
Hi experts, i'm having a vba code to add a new row to my table, if my first row is blank, it need to run twice to add the second row, if my first row is not blank it only need one time.
Thank you in advance.

VBA Code:
Sub AddRow()
    Dim pswStr                                    As String
    pswStr = "@"
    On Error Resume Next
    Application.ScreenUpdating = False
    ActiveSheet.Unprotect Password:=pswStr
    Dim ws                                        As Worksheet    
    Set ws = ActiveSheet
    Dim tbl                                       As ListObject
    
    Set tbl = ws.ListObjects("TB_ReOpen")
    'add a row at the end of the table
    tbl.ListRows.Add
    ActiveSheet.Protect Password:=pswStr
End Sub
 

Some videos you may like

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,301
Office Version
  1. 365
Platform
  1. Windows
How about
VBA Code:
    Dim r As Range
    
    Set tbl = ws.ListObjects("TB_ReOpen")
    Set r = tbl.DataBodyRange
    If r Is Nothing Then tbl.ListRows.Add
    'add a row at the end of the table
    tbl.ListRows.Add
    ActiveSheet.Protect Password:=pswStr
 

bumbum2812

New Member
Joined
Sep 7, 2020
Messages
25
Office Version
  1. 365
Platform
  1. Windows
How about
VBA Code:
    Dim r As Range
   
    Set tbl = ws.ListObjects("TB_ReOpen")
    Set r = tbl.DataBodyRange
    If r Is Nothing Then tbl.ListRows.Add
    'add a row at the end of the table
    tbl.ListRows.Add
    ActiveSheet.Protect Password:=pswStr
Thank you Sir, have a good day!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,301
Office Version
  1. 365
Platform
  1. Windows
Glad to help & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,098
Messages
5,622,683
Members
415,920
Latest member
ExcelNoob28

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
Top