Results 1 to 6 of 6

Inserting row into excel table in protected sheet

Threaded View

  1. #1
    Registered User
    Join Date
    01-19-2020
    Location
    Hungary
    MS-Off Ver
    Office 2007
    Posts
    19

    Inserting row into excel table in protected sheet

    Hi,

    Edit Mod : this thread is related to https://www.excelforum.com/excel-pro...ml#post5267116

    I have an excel table in a protected Excel sheet. I should be able to insert row(s) in the table but it is not possible because the sheet is protected therefore I got warning message ("The cell or chart you're trying to change is on a protected sheet. To make a change, unprotect the sheet. You might be requested to enter a password."). I set AllowInsertingRows:=True but it does not help because this an Excel table. Do you know how I can handle this by macro? I tried using the following macro but not working. Can you please help to implement this in the attached table or find another solution?


    Function IsCellInTable(cell As Range) As Boolean
    'PURPOSE: Determine if a cell is within an Excel Table
    
    
    IsCellInTable = False
    
    On Error Resume Next
      IsCellInTable = (cell.ListObject.Name <> "")
    On Error GoTo 0
       
    End Function
    
    Private Sub AddTableRows()
    'PURPOSE: Add table row based on user's selection
    
    
    Dim rng As Range
    Dim InsertRows As Long
    Dim StartRow As Long
    Dim InsideTable As Boolean
    Dim RowToBottom As Boolean
    Dim ReProtect As Boolean
    Dim Password As String
    Dim area As Range
    
    'Optimize Code
      Application.ScreenUpdating = False
    
    'What is the worksheet password?
      Password = "Nemzetisport89"
    
    'Set Range Variable
      On Error GoTo InvalidSelection
        Set rng = Selection
      On Error GoTo 0
    
    'Unprotect Worksheet
      With ActiveSheet
        If .ProtectContents Or .ProtectDrawingObjects Or .ProtectScenarios Then
          On Error GoTo InvalidPassword
          .Unprotect Password
          ReProtect = True
          On Error GoTo 0
        End If
      End With
    
    'Loop Through each Area in Selection
      For Each area In rng.Areas
    
        'Is selected Cell within a table?
          InsideTable = IsCellInTable(area.Cells(1, 1))
          
        'Is selected cell 1 row under a table?
          RowToBottom = IsCellInTable(area.Cells(1, 1).Offset(-1))
        
        'How Many Rows In Selection?
          InsertRows = area.Rows.Count
        
        'Selection Not Within Table?
          If Not InsideTable And Not RowToBottom Then GoTo InvalidSelection
        
        'Add Rows To Table
          If InsideTable Then
          
            'Which Row in Table is selected?
              With area.Cells(1, 1)
                x = .Row
                y = .ListObject.DataBodyRange.Row
                Z = .ListObject.DataBodyRange.Rows.Count
              End With
              
              StartRow = Z - ((y + Z - 1) - x)
              
            'Insert rows based on how many rows are currently selected
              For x = 1 To InsertRows
                area.ListObject.ListRows.Add (StartRow)
              Next x
          ElseIf RowToBottom Then
            For x = 1 To InsertRows
              area.Cells(1, 1).Offset(-1).ListObject.ListRows.Add AlwaysInsert:=True
            Next x
          End If
    
      Next area
    
    'Protect Worksheet
      If ReProtect = True Then ActiveSheet.Protect Password
    
    Exit Sub
    
    'ERROR HANDLERS
    InvalidSelection:
      MsgBox "You must select a cell within or directly below an Excel table"
      If ReProtect = True Then ActiveSheet.Protect Password
      Exit Sub
    
    InvalidPassword:
      MsgBox "Failed to unlock password with the following password: " & Password
      Exit Sub
      
    End Sub
    Attached Files Attached Files
    Last edited by Balazs Batta; 01-27-2020 at 04:56 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 11
    Last Post: 01-29-2019, 09:53 PM
  2. [SOLVED] Inserting New Rows in Protected Table
    By RSa in forum Excel Charting & Pivots
    Replies: 13
    Last Post: 02-02-2016, 04:36 AM
  3. Excel table and protected sheet
    By cameron213 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-14-2013, 12:54 AM
  4. Inserting a new row in protected sheet
    By harry$100 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-11-2013, 12:25 PM
  5. Help Inserting a row in a protected sheet using a macro
    By mrpaine in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-13-2012, 10:30 PM
  6. Inserting in a protected sheet
    By kc27315 in forum Excel General
    Replies: 1
    Last Post: 12-18-2008, 09:10 AM
  7. Inserting Rows on a Protected Sheet
    By redstang423 in forum Excel General
    Replies: 1
    Last Post: 05-07-2008, 03:01 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1