Excel 如何使用函数作为参数?

Excel 如何使用函数作为参数?,excel,vba,Excel,Vba,我想要一个函数,将取消保护和重新保护我的工作表 我目前的职能如下: Public Function RunProtect(fun As Function, sheet As Worksheet) Dim protected As Boolean: protected = False If sheet.ProtectContents = True Then protected = True sheet.Unprotect End If

我想要一个函数,将取消保护和重新保护我的工作表

我目前的职能如下:

Public Function RunProtect(fun As Function, sheet As Worksheet)
    Dim protected As Boolean: protected = False
    If sheet.ProtectContents = True Then
        protected = True
        sheet.Unprotect
    End If

    'Code to run fun 

    If protected = True Then
        sheet.protect
    End If
End Function

这是可能的还是有更简单的方法?在使用VBA编辑表格时,我必须取消对工作表的保护。

使用
UserInterfaceOnly:=true进行一次保护,您不需要每次都取消保护-格斯伯格


谢谢大家!

使用
UserInterfaceOnly:=true
保护一次,您就不需要每次都取消保护。太棒了!谢谢,先生。您必须在类模块中编写函数。然后它将具有与工作表类似的属性(可能是方法)。假设您将类命名为Funky,那么您将在这个函数中输入“fun as Funky”。但是如果有人要这样做,你必须展示函数的代码。@GSerg你不知道我现在的想法有多激动,我至少有一个月的时间遇到同样的问题,谢谢你,先生。虽然你对自己的问题给出了答案,但你仍然可以“接受”它-这让其他人知道你不再需要积极的帮助。然而,这并没有结束这个问题,仍然允许其他人发布答案以供将来阅读。