宏当文件被保护运行

问题描述 投票:1回答:2

我这样做,创建一个图形宏,但需要做保护的文件密码。

当然,当我保护的文件,宏将停止工作。

我插入下面的到我的代码来取消保护文件,运行代码,然后再保护的文件。正如我的代码是一个函数,我不得不创建两个子过程,这也许是为什么招不正常的原因。

任何想法,我怎么能解决这个问题?

Option Explicit
Sub protection()

    Worksheets("Sheet1").Unprotect "abc123"

End Sub

Function (here is my function code)

End Function

Sub protection2()
Worksheets("Sheet1").protect "abc123"

End Sub
excel vba plot
2个回答
2
投票

我猜你想开始一个子过程做的伎俩。我的例子取消保护您的工作表,让功能做它的魔力和保护工作表。

Option Explicit
Sub protection()

    Worksheets("Sheet1").Unprotect "abc123"

    Call Function (here may be values for your arguments)

    Worksheets("Sheet1").protect "abc123"
End Sub

Function (here may be prameters)

    the function code belongs here

End Function

1
投票

在一个受保护的表,你不能改变其锁定的单元格。您可能wan't玩弄附带的代码:

Option Explicit

Sub SheetSetup()
    Range("B3:C7").Locked = False
    Range("E3:F7").Locked = True   'This is default
End Sub

Sub Sample_ProtectedSheet()
    ClearValues
    ChangeAllValues_on_ProtectedSheet
    MsgBox ("Only values in ""B4:C7"" are set to ""yes""!")
End Sub

Sub Sample_UnprotectedSheet()
    ClearValues
    ChangeAllValues_on_UnprotectedSheet
    MsgBox ("All values set to ""yes""!")
End Sub


Function ChangeAllValues_on_UnprotectedSheet()
    Call Unprotect
    On Error Resume Next
    Range("B4:C7").Value = "yes"
    Range("E4:F7").Value = "yes"
    On Error GoTo 0
End Function

Function ClearValues()
    Call Unprotect
    On Error Resume Next
    Range("B4:C7").Value = ""
    Range("E4:F7").Value = ""
    On Error GoTo 0
End Function

Function ChangeAllValues_on_ProtectedSheet()
    Call Protect
    On Error Resume Next
    Range("B4:C7").Value = "yes"
    Range("E4:C7").Value = "yes"
    On Error GoTo 0
End Function

Sub Protect()
    Worksheets("Sheet1").Protect "abc123"
End Sub

Sub Unprotect()
    Worksheets("Sheet1").Unprotect "abc123"
End Sub

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.