简体   繁体   English

允许 Excel 宏在受保护的工作表上运行

[英]Allow Excel Macros to Run On a Protected Sheet

I have the following macro that is not able to run whenever I protect the sheet.每当我保护工作表时,我都有以下宏无法运行。

Sub ConditionalDisplay()
    With Worksheets("Data Entry")
    If .Range("C13") = "" Then
        .Rows("14:15").Hidden = True
      Else
        .Rows("14").Hidden = .Range("C13") = "lbs/gal"
        .Rows("15").Hidden = .Range("C13") = "g/L"
    End If

    If .Range("C17") = "" Then
        .Rows("18:19").Hidden = True
        Else
            .Rows("18").Hidden = .Range("C17") = "lbs/gal"
            .Rows("19").Hidden = .Range("C17") = "g/L"
    End If
End With
End Sub

If it's unprotected it works fine, but protecting the sheet throws an error when I open the workbook as this sub routine is called when the workbook opens.如果它没有受到保护,它可以正常工作,但是当我打开工作簿时保护工作表会引发错误,因为当工作簿打开时会调用此子例程。 I saw a couple of websites that reference using the "Me.Protect Userinterfaceonly:=true" but so far that has not worked out for me.我看到几个使用"Me.Protect Userinterfaceonly:=true"引用的网站,但到目前为止,这对我来说还没有成功。

Are there any suggestions?有什么建议吗?

For anyone wondering, below is the solution:对于任何想知道的人,以下是解决方案:

Private Sub Workbook_Open()
    Worksheets("Data Entry").Protect Userinterfaceonly:=True
    Worksheets("Data Entry").Range("C6:C10") = ""
    Worksheets("Data Entry").Range("C12:C15") = ""
    Worksheets("Data Entry").Range("C17:C19") = ""
ConditionalDisplay
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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