简体   繁体   中英

Unprotect sheet in MS Excel for ActiveX controls

I implemented smart dropdown (using Active X controls) in my sheet, so it is protected.
This functionality is not able to work because of protection.

When I right click on the sheet and Unprotect it, providing the password, it works.
I do not want the user to do this manually. I want that when user opens the sheet the sheet should get unprotected using VBA code.

The entire cells in the sheet are Locked (Format cells-> Protection -> Locked marked).
I cannot untick the Locked mark, as there are some validations which gets failed and not allow me to upload such template.

Below is an example as I cannot share the application code.

在此处输入图像描述

VBA code:

In sheet1-> name of sheet is "tab0"

Private Sub Worksheet_BeforeDoubleClick _
  (ByVal Target As Range, _
  Cancel As Boolean)
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Set ws = ActiveSheet

ws.Unprotect Password:="xxx"

在此处输入图像描述

Inside ThisWorkbook I have this

Private Sub Workbook_Open() 
    ThisWorkbook.UnProtect Password = "password" 
End Sub

A late input to this question:

I've been experiencing similar problems with Unprotecting sheets, and ended up doing aa fair bit of testing to find what works and what doesn't (always.).

The problem appears to be when assigning the sheet to an object and using the object's dot notation to unprotect e,g.:

'Setting the worksheet (globally) in sub Main()
Public goXsTT As Worksheet                      
    Set goXsTT = goXbWb.Sheets(2)

'Somewhere else in the code
    goXsTT.Unprotect

'This ***may*** fail, whereas:

    Sheets(2).Unprotect
'Works!

No idea why this is the case - just pointing out that it's a workaround if you're stumped, as I was. Hope it helps. DG

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM