Insert a new row, keeping data validation - Macro/Button

Tony Montana

New Member
Joined
Aug 10, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi all!

I am looking to add a button to a spreadsheet which will insert a new row at the top of the table (row 2), whilst keeping the data validation & formatting of the row below.

You will see in the example excel doc which I've attached that I have created an array alongside the table. This is done to prevent others from deleting rows, and does the job. I would like to keep this array (or any alternate which works with the "add row button") that will protect rows in the table from being deleted.

I would really appreciate some help ?

Example.xlsx
ABCDEFGHIJK
1RegionDate EnteredCompanySectorClassificationContact1
2East10/08/2021ABC EnterpriseEngineeringABill1
3East09/08/2021IT ServicesTechBRoxanne1
4West09/08/2021Bulk WholesaleFoodAJane1
51
61
71
81
91
101
111
121
131
141
151
161
171
181
191
201
211
221
231
241
251
26
27
28
29
30
31
Sheet1
Cell Formulas
RangeFormula
I1:I25I1=1
Press CTRL+SHIFT+ENTER to enter array formulas.
Cells with Data Validation
CellAllowCriteria
A2:A25ListNorth, South, East, West
D2:D25ListManufacturing, Engineering, Tech, Food
E2:E25ListA, B, C
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
7,552
I guess you are preventing rows in the table being deleted by locking cells I1:I25 and protecting the sheet.

A better approach is to use 'Allow Edit Ranges' on the table's data body range, which will prevent rows being deleted but allow the cells to be edited. First, run this macro once to set up the 'Allow Edit Ranges' on the table's data body range, lock its header row and protect the sheet.

VBA Code:
Public Sub Lock_Table()

    Dim table As ListObject
    Dim editRange As AllowEditRange
   
    With ActiveSheet
   
        .Unprotect Password:="xxxx"
       
        'Unlock all cells on sheet
       
        .Cells.Locked = False
       
        Set table = .ListObjects(1)
       
        'Lock table's header row
       
        table.HeaderRowRange.Locked = True
       
        'Delete table's current edit range, named "TableData", if it exists
       
        For Each editRange In .Protection.AllowEditRanges
            If editRange.Title = "TableData" Then editRange.Delete
        Next
       
        'Add new edit range named "TableData" and assign it to table's data body
       
        .Protection.AllowEditRanges.Add Title:="TableData", Range:=table.DataBodyRange
       
        'Protect sheet with password.  UserInterfaceOnly:=True means other VBA procedures don't need to Unprotect the sheet to insert rows or change cells etc.
       
        .Protect UserInterfaceOnly:=True, Password:="xxxx", DrawingObjects:=False, Contents:=True, Scenarios:=True
   
    End With
   
End Sub
Now you can run the following macro to insert a new top row in the table:
VBA Code:
Public Sub Insert_Top_Row()

    Dim table As ListObject
   
    With ActiveSheet
        '.Unprotect  'not needed because sheet was protected with UserInterfaceOnly:=True in Lock_Table macro
        Set table = .ListObjects(1)
        table.DataBodyRange.Rows(1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrBelow
    End With

End Sub
 
Solution

Tony Montana

New Member
Joined
Aug 10, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
This is incredible, all works and am very happy.

Thank you John
 

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
7,552
I think you'll find that the Insert_Row macro doesn't work after you close and reopen the workbook without running the Lock_Table macro again. Lock_Table is only meant to be run once to set up the protection on the table.

The problem is that .Protect UserInterfaceOnly:=True doesn't persist in the saved workbook and must be 'redone' when the workbook is opened. Fix this by adding this code to the ThisWorkbook module:
VBA Code:
Private Sub Workbook_Open()    
    ThisWorkbook.Worksheets("Sheet1").Protect UserInterfaceOnly:=True, Password:="xxxx", DrawingObjects:=False, Contents:=True, Scenarios:=True
End Sub
where "Sheet1" is the name of the sheet containing the table and "xxxx" is the password used to protect that sheet.
 

Forum statistics

Threads
1,176,276
Messages
5,902,292
Members
434,959
Latest member
etb1025

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