将宏运行到受保护的工作表 - 未找到其他答案

时间:2013-11-08 12:32:18

标签: excel excel-vba vba

这里......

我在1工作簿中创建了一个由31个工作表组成的项目表单。 名为“NOV摘要”的工作表1是摘要页面。其他30个工作表都有各种详细信息。

我创建的宏与30个工作表上的选项按钮有关,当选择在“NOV摘要”页面上放置信息时。

此摘要页面基本上是在用户在其他工作表上输入信息时自动创建和填写的。因此,我想保护“NOV摘要”页面,但仍然运行marcos。

我的vba知识是最基本的,我无法在所有搜索后解决。

以下是我正在使用的马科斯的一个例子---

子说明符()

” '说明者宏 “ “

Sheets("NOV Summary").Select
Range("D4").Select
ActiveCell.FormulaR1C1 = "Specifier"
With ActiveCell.Characters(Start:=1, Length:=9).Font
    .Name = "Calibri"
    .FontStyle = "Regular"
    .Size = 11
    .Strikethrough = False
    .Superscript = False
    .Subscript = False
    .OutlineFont = False
    .Shadow = False
    .Underline = xlUnderlineStyleNone
    .ThemeColor = xlThemeColorLight1
    .TintAndShade = 0
    .ThemeFont = xlThemeFontMinor
End With
Range("D4").Select
Sheets("PR1311001").Select

End Sub

如果您可以帮助我们放置正确的代码以便实现这一点,我们将不胜感激!

嗯,谢谢

汤姆

1 个答案:

答案 0 :(得分:4)

您可以在运行宏之前取消保护工作表,然后再次保护它

Public Sub PasswordTest()


Dim password As String
Dim ws As Worksheet

Set ws = Sheet1

password = "password123"

'Check if sheet is protected
If ws.ProtectContents Then

    ws.Unprotect password

End If

         '*******
         'put your code here
         '*******

'Protect sheet again
ws.Protect password


Exit Sub

'error handling block
err:

MsgBox err.Description, "an error occured"

'if the sheet isn't protected then we should protect it again.
If ws.ProtectContents = False Then

    ws.Protect password

End If


End Sub

正如评论中所指出的,您可以在保护工作表时设置UserInterFaceOnly参数。这将保护它,但允许宏仍然可以使用它。

ws.Protect Password:=password, UserInterFaceOnly:=True

你可以在这里阅读..

http://www.ozgrid.com/VBA/excel-macro-protected-sheet.htm