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

bumbum2812

New Member
Joined
Sep 7, 2020
Messages
26
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
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
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
 
Upvote 0
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!
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,262
Members
449,075
Latest member
staticfluids

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