简体   繁体   中英

Excel VBA protect returning error

I've seen many posts on here and elsewhere about using the following code in the ThisWorkbook Object in order to protect your workbook but still allow macros to run:

Private Sub Workbook_Open()
    ActiveWorkbook.Protect UserInterfaceOnly:=True
End Sub

However, the protect function tooltip that pops up only offers three arguments Protect([password], [structure], [windows]). Therefore, when I open the document I get the following error

"Compile Error: Named argument not found"

in regard to UserInterfaceOnly:=True .

Am I using the wrong protect function, or is there something else I'm missing? I want to allow users to input data into the workbook using forms/macros, but I don't want them to be able to manually change any cells.

It almost seems as if the protect function that I'm running has different arguments than many of the other protect solutions I've found. Could this be a version issue or something else?

The suggestion provided by Tim Williams worked. Protect doesn't work the same way at the workbook level, therefore I needed to write individual code for each sheet I wanted to protect. I used the following code to protect the sheets but allow forms to populate:

Private Sub Workbook_Open()
    Worksheets("Sheet1").Protect UserInterfaceOnly:=True
    Worksheets("Sheet2").Protect UserInterfaceOnly:=True
    Worksheets("Sheet3").Protect UserInterfaceOnly:=True
End Sub

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