Invoke VBA to check if the sheet is protected in excel?

Hi all,

What is VBA code to check if the sheet is protected in excel ?

I used this code below, the output was null

Function IsSheetProtected(sheetName As String) As Boolean
Dim ws As Worksheet
On Error Resume Next
Set ws = Worksheets(sheetName)
On Error GoTo 0

If Not ws Is Nothing Then
    IsSheetProtected = ws.ProtectContents
Else
    ' If the sheet does not exist, consider it as protected
    IsSheetProtected = True
End If

End Function

Hi @Suastrian_Dika

Use the invoke vba activity to check the excel sheet is protected or not. Use the below vb code.

Function IsSheetProtected(sheetName As String) As Boolean
    Dim ws As Worksheet
    On Error Resume Next
    Set ws = Worksheets(sheetName)
    On Error GoTo 0
    
    If Not ws Is Nothing Then
        IsSheetProtected = ws.ProtectContents
    Else
        IsSheetProtected = False
    End If
End Function

Hope it helps!!

Hi,
Try below code

Function IsSheetProtected(sheetName As String) As Boolean
Dim ws As Worksheet
On Error Resume Next
Set ws = Worksheets(sheetName)
On Error GoTo 0

If Not ws Is Nothing Then
    IsSheetProtected = ws.ProtectContents Or ws.ProtectDrawingObjects _
                        Or ws.ProtectScenarios Or ws.ProtectionMode
Else
    ' If the sheet does not exist, consider it as protected
    IsSheetProtected = True
End If

End Function

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.