【问题标题】:How to keep macro running when there are protected sheets?有受保护的工作表时如何保持宏运行?
【发布时间】:2017-09-11 02:09:21
【问题描述】:

我用密码保护了表格 4,因为有些单元格不允许用户在表格 4 的这些单元格中输入。密码是 1234。

但是,我想运行我的宏,如果出现错误,单元格将自动突出显示。

我的宏没有运行并且出错,因为我要突出显示的单元格在受保护的工作表中。

当我点击验证按钮时,如何使工作表 4 保持受保护并让我的宏继续运行?

Private Sub commandbutton1_click()

FileFormat:=xlOpenXMLWorkbookMacroEnabled, Password:=1234, WriteResPassword:=1234, _
    ReadOnlyRecommended:=False, CreateBackup:=False

vehicle = Sheets("4").Range("K22")

expenditure_gasoline = Sheets("4").Range("M22")


If vehicle = true and expenditure_gasoline = 0 Then
        MsgBox "it should not be empty", vbcritical

End If

If vehicle = true and expenditure_gasoline = 0 Then Sheets("4").Range("M22").Interior.ColorIndex = 3


End sub

【问题讨论】:

  • 您需要在更改之前取消保护工作表(使用Sheets("4").Unprotect "1234"),进行颜色更新,然后使用Sheets("4").Protect "1234" 保护它,或者只执行一次允许编辑的保护通过 VBA 像这样:Sheets("4")Protect Password:="1234", UserInterfaceOnly:=True

标签: vba excel validation


【解决方案1】:

尝试以下更改(未经测试)


V1 - 保护工作表免受用户更改,而不是 VBA 更改 UserInterfaceOnly:=True


Option Explicit

Private Sub commandbutton1_click()

    Const PATH_AND_FILENAME = "C:\YourTestFile.xlsx" '<------ Update this path & file name

    Dim wb As Workbook, ws As Worksheet, vehicle As Variant, expenditureGasoline As Variant

    Set wb = Workbooks.Open(Filename:=PATH_AND_FILENAME, WriteResPassword:="1234", _
                            Password:="1234", Format:=xlOpenXMLWorkbookMacroEnabled)
    Set ws = wb.Sheets("4")

    ws.Protect Password:="1234", UserInterfaceOnly:=True '<--- Protect changes from UI only

    Set vehicle = ws.Range("K22")
    Set expenditureGasoline = ws.Range("M22")

    If Not IsError(vehicle) And Not IsError(expenditureGasoline) Then
        If vehicle = True And expenditureGasoline = 0 Then
            ws.Range("M22").Interior.ColorIndex = 3
            MsgBox "Cell M22 should not be empty", vbExclamation
        End If
    End If
End Sub

V2 - 更改前取消保护,更改后重新保护


Private Sub commandbutton1_click()

    Const PATH_AND_FILENAME = "C:\YourTestFile.xlsx" '<------ Update this path & file name

    Dim wb As Workbook, ws As Worksheet, vehicle As Variant, expenditureGasoline As Variant

    Set wb = Workbooks.Open(Filename:=PATH_AND_FILENAME, WriteResPassword:="1234", _
                            Password:="1234", Format:=xlOpenXMLWorkbookMacroEnabled)
    Set ws = wb.Sheets("4")
    Set vehicle = ws.Range("K22")
    Set expenditureGasoline = ws.Range("M22")

    If Not IsError(vehicle) And Not IsError(expenditureGasoline) Then
        If vehicle = True And expenditureGasoline = 0 Then

            ws.Unprotect "1234"                     '<--- Unprotect it before the change
            ws.Range("M22").Interior.ColorIndex = 3
            ws.Protect "1234"                       '<--- Protect it back, after the change

            MsgBox "Cell M22 should not be empty", vbExclamation
        End If
    End If
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-11
    • 2020-10-05
    • 1970-01-01
    • 1970-01-01
    • 2020-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多