Check if a VBProject is protected using VBA in Microsoft Excel

With the function below you can check if a VBProject is protected before you try to edit the project:

Function ProtectedVBProject(ByVal wb As Workbook) As Boolean
' returns TRUE if the VB project in the active document is protected
Dim VBC As Integer
    VBC = -1
    On Error Resume Next
    VBC = wb.VBProject.VBComponents.Count
    On Error GoTo 0
    If VBC = -1 Then
        ProtectedVBProject = True
        ProtectedVBProject = False
    End If
End Function


If ProtectedVBProject(ActiveWorkbook) Then Exit Sub


  1. "Hi, I simply want to protect my VBA code in Excel from people. They must still be able to use the worksheets as they see fit but not see my code. Anyone know how I do this?

    If your reply is password protection, how do I do that? I also understand that a determined hacker could probaly get to the code anyway."

Leave a Reply

Your email address will not be published. Required fields are marked *

Terms and Conditions of use

The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.