How to keep macro running when there are protected sheets?

烈酒焚心 提交于 2021-02-05 07:16:56

问题


I have protected sheets 4 with a password because there are some cells that users aren't allowed to input in those cells in sheet 4. The password is 1234.

But, I want to run my macro, and if there is an error, the cell will be highlight automatically.

My macro doesn't run and error, because the cell that I want to highlight is in protected sheet.

How to make the sheet 4 stay protected and make my macro keep running when I click the validation button?

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

回答1:


Try the changes bellow (untested)


V1 - Protect the sheet from user changes, but not VBA changes 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 - Unprotect before the change, and Protect back after the change


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


来源:https://stackoverflow.com/questions/46147302/how-to-keep-macro-running-when-there-are-protected-sheets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!