December 7, 2021
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
Power Query
Power Pivot
Xtreme Pivot Tables
Excel for Decision Making
Excel for Finance
Excel Analysis Toolpak
Power BI
Excel
Word
Outlook
Excel Expert
Excel Customer Service
PowerPoint
November 8, 2013
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:
Answers Post
Power Query
Power Pivot
Xtreme Pivot Tables
Excel for Decision Making
Excel for Finance
Excel Analysis Toolpak
Power BI
Excel
Word
Outlook
Excel Expert
Excel Customer Service
PowerPoint
November 8, 2013
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.
1 Guest(s)