cancel
Showing results for 
Search instead for 
Did you mean: 

Protect BPC with VBA code

Former Member
0 Kudos
460

Hi. How i can protect sheet with code VBA. I want to do etools---- work book option---- set workbook password... But i want this with code VBA.

Thanks.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

I use vba a lot to protect and unprotect BPC documents.

You only have to do it once manually using etools-workbookoptions. After this you can use the standard excel method with the SAME password.

Below an example of vba code applying (un)protection using the excel option, I use this code to easely unprotect/protect an evdre sheet including unhiding sections and setting a freeze pane:


Sub LockWB()
'
' Macro2 Macro
' De macro is opgenomen op 12-12-2008 door ABT.
'
' Sneltoets: CTRL+SHIFT+L
'

frcell = Range("FreezeCell")

With ActiveWorkbook
    ActiveSheet.Outline.ShowLevels RowLevels:=1, ColumnLevels:=1
    Range(frcell).Select
    ActiveWindow.FreezePanes = True
    With ActiveWindow
        .DisplayHeadings = False
        .DisplayOutline = False
        .DisplayWorkbookTabs = False
    End With
    ActiveSheet.Protect ("MYPASSWORD")
    
End With
End Sub




Sub UnlockWB()
'
' Macro3 Macro
' De macro is opgenomen op 12-12-2008 door ABT.
'
' Sneltoets: CTRL+SHIFT+U
'

ActiveSheet.Unprotect ("MYPASSWORD")

frcell = Range("FreezeCell")

With ActiveWorkbook
    
    ActiveSheet.Outline.ShowLevels RowLevels:=4, ColumnLevels:=4
    ActiveWindow.FreezePanes = False
    With ActiveWindow
        .DisplayHeadings = True
        .DisplayOutline = True
        .DisplayWorkbookTabs = True
    End With
End With

Hope this helps,

Alwin

Former Member
0 Kudos

Thanks

Answers (1)

Answers (1)

i045436
Advisor
Advisor
0 Kudos

Hello,

Unfortunatelly, this is not possible using the "Workbook option ..." menu.

To protect your code, open the Excel Workbook and go to Tools>Macro>Visual Basic Editor (Alt+F11). Now, from within the VBE go to Tools>VBAProject Properties and then click the Protection page tab and then check "Lock project from viewing" and then enter your password and again to confirm it. After doing this you must save, close & reopen the Workbook for the protection to take effect.

Best regards,

Mihaela