Forum

Run-time error -214...
 
Notifications
Clear all

Run-time error -2147024809 (80070057) - You cannot use this command on a protected sheet.... but the sheet is NOT protected!

4 Posts
2 Users
0 Reactions
323 Views
(@swallack202)
Posts: 77
Estimable Member
Topic starter
 

I have a module with the following code called from a button on the sheet. Even if the sheet is unlocked (which I have checked both programmatically through VBA and manually by right-clicking the sheet tab), I get an error saying that the sheet is protected. And the error number is negative! It's "-2147024809 (80070057)." 

Option Explicit

Public Type SlicerFormatSettings
  Name As String
  DisplayHeader As Boolean
  Caption As String
  Top As Single
  Left As Single
  Width As Single
  Height As Single
  NumberOfColumns As Long
End Type


Sub PrepWork()

 ' Unprotect the sheet before doing the work
  Dim Unlocked As Boolean
  Unlocked = LockSheet(ActiveSheet.Index, False)

  Dim FormatSettings As SlicerFormatSettings
  Dim SlicerFormat As SlicerFormatSettings

  ' Set slicer properties
  With SlicerFormat
    .Name = "NEW_SLICER"
    .DisplayHeader = True
    .Caption = "Slicer Caption"
    .Top = Application.InchesToPoints(1.25)
    .Left = Application.InchesToPoints(5)
    .Width = Application.InchesToPoints(1.25)
    .Height = Application.InchesToPoints(1.5)
    .NumberOfColumns = 1
  End With

  ' Apply properties to the slicer
  Call SetSlicerProperties

  ' Protect the sheet after doing the work
  Dim Locked As Boolean
  Locked = LockSheet(ActiveSheet.Index, True)

End Sub

Sub SetSlicerProperties( _
    ByVal myName As String, _
    ByVal myDisplayHeader As Boolean, _
    ByVal myCaption As String, _
    ByVal myTop As Single, _
    ByVal myLeft As Single, _
    ByVal myWidth As Single, _
    ByVal myHeight As Single, _
    ByVal myNumberOfColumns As Long)

  ' Make sure the sheet is unlocked
  If Islocked(ActiveSheet.Index) Then Msgbox "Try again. The sheet is locked." : Exit Sub

  Dim mySlicer As Slicer

  ' Get the slicer object (or exit)
  Set mySlicer = GetSlicer(myName)
  If mySlicer Is Nothing Then
    MsgBox "That slicer was not found"
    Exit Sub
  End If

  With mySlicer
    ' Set slicer dimensions
    .NumberOfColumns = SlicerFormat.NumberOfColumns   <---- ERROR HAPPENS HERE

   ' Set slicer position
    With .Shape
      .Top = SlicerFormat.Top
      .Left = SlicerFormat.Left
      .Width = SlicerFormat.Width
      .Height = SlicerFormat.Height
      .Placement = xlFreeFloating
      .locked = False
    End With ' Shape

    ' Set slicer caption, style, display header choice, sort and filter options, and lock option  
    With mySlicer
      .Caption = SlicerFormat.Caption
      .Style = "SlicerStyleDark5"
      .DisplayHeader = SlicerFormat.DisplayHeader
      .SlicerCacheLevel.SortItems = xlSlicerSortAscending
      .SlicerCacheLevel.CrossFilterType = xlSlicerSortAscending ' xlSlicerCrossFilterHideButtonsWithNoData
      .DisableMoveResizeUI = True
    End With 'slcr

  End With ' mySlicer

End Sub

Function IsLocked(ByVal iSheetIndex As Integer) As Boolean

  ' Return true if sheet is protected, false if not
  IsLocked = ActiveSheet.ProtectContents

End Function

Function LockSheet(ByVal iSheetIndex As Integer, bSwitch As Boolean) As Boolean

  LockSheet = False

  Dim SecretPassword as String
  SecretPassword = "12345"
  
  ' Protect or unprotect the sheet
  If bSwitch Then ' Protect
    ThisWorkbook.Worksheets(iSheetIndex).Protect Password:=SecretPassword, DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True

  Else ' Unprotect
    ThisWorkbook.Worksheets(iSheetIndex).Unprotect Password:=SecretPassword

  End If

  If Err.Number = 0 Then LockSheet = True

End Function

Function GetSlicer(sName As String) As Slicer

  Dim N As Long
  Dim C As Long

  ' In case of run-time error, move to next statement and continue execution
  On Error Resume Next

  C = ActiveWorkbook.SlicerCaches.Count
  For N = 1 To C
    Set GetSlicer = ActiveWorkbook.SlicerCaches(N).Slicers(sName)
    If Not GetSlicer Is Nothing Then Exit Function
    Debug.Print "GetSlicer " & N & " of " & C & ": " & sName
  Next N

End Function

Excel-Negative-Error.JPGExcel-Code-Snippet.JPG
 
Posted : 04/06/2022 5:18 pm
(@catalinb)
Posts: 1937
Member Admin
 

The IsLocked function is wrong.

Function IsLocked(ByVal iSheetIndex As Integer) As Boolean

  ' Return true if sheet is protected, false if not
  IsLocked = ActiveSheet.ProtectContents

End Function

It is checking the active sheet, NOT the sheetIndex argument passed to the function.

Hard to evaluate based on images, you should try that sometime 🙂

If you cannot prepare a sample file that replicates the error, you can send the file to us using our helpdesk, where your file remains private:

https://www.myonlinetraininghub.com/helpdesk/

 
Posted : 07/06/2022 12:54 am
(@swallack202)
Posts: 77
Estimable Member
Topic starter
 

THANK YOU! It was right there and I missed it.

And... yes, I am trying to get better (and more thoughtful) about creating shareable sample files.

As always... much appreciated.

 
Posted : 07/06/2022 4:12 pm
(@catalinb)
Posts: 1937
Member Admin
 

Hi Shawn,

The error comes from the fact that you are wrongly using the Disable Resize from UI.

Instead of:

' Enable resizing and moving slicers
Call EnableResizeMoveSlicers(sheetindexnumber, True)

' Align slicers vertically
Call SpaceSlicers(sheetindexnumber)

' Disable resizing and moving slicers
Call EnableResizeMoveSlicers(sheetindexnumber, False)

 

It should be the opposite, set it to False before calling SpaceSlicers to enable resizing.

Setting the option to True DISABLES the resizing.

' Enable resizing and moving slicers
Call EnableResizeMoveSlicers(sheetindexnumber, False)

' Align slicers vertically
Call SpaceSlicers(sheetindexnumber)

' Disable resizing and moving slicers
Call EnableResizeMoveSlicers(sheetindexnumber, True)

It's a confusion caused by misleading function names. It should be named DisableResizeMoveSlicers:

DisableResizeMoveSlicers(sheetindexnumber, False) to allow resizing

DisableResizeMoveSlicers(sheetindexnumber, True) to disable resizing

 

You are excessively using The Error Resume Next statement.

You should allow errors to break the execution, this will force you to analyze the cause then MANAGE the cause of the error properly.

Hiding the errors under the carpet does not mean that everything is right, you should use it ONLY when you know exactly what error you want to avoid or ignore.

To debug your app, I disabled all these statements (lots of them), after fixing the problem described above, the code works without enabling them back.

 
Posted : 12/06/2022 3:33 am
Share: