excel - 工作簿受保护时样式子例程失败
问题描述
我真的很难理解为什么如果工作簿受到保护,设置样式的子例程将不起作用。我曾尝试在子例程之前解锁特定页面,但这失败了。我尝试使用UserInterFaceOnly:=True锁定工作簿,但这也不起作用!
我为打开工作簿时设置了这个例程。
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In Worksheets
ws.Protect Password:="Password", _
UserInterFaceOnly:=True
Next ws
Sheets("Menu").Select
Range("A1").Select
End Sub
我的子程序是这个
Sub InputStyleRestore()
With ActiveWorkbook.Styles("Input")
.Interior.Color = 10079487
.Font.Color = -9027777
End With
End Sub
当工作簿被锁定时,我收到“运行时错误 1004 - 应用程序定义或对象定义错误”并且该行 .Interior.Color = 10079487
被突出显示。
我希望暂时解锁页面的例程的开始是这样开始的
Sub PartialPrintFamForm()
Dim FTW As Long
Dim myVariable As Long
Dim IsCreated As Boolean
Dim i As Long
Dim PdfFile As String, Title As String
Dim OutlApp As Object
ActiveSheet.Unprotect Password:="Password"
InputStyleRestore
' Employee Name as Title
Title = Range("E21")
If ActiveSheet.Name = "Caledonian Road Fam Form" Then
myVariable = Sheets("Caledonian Road Fam Form").Range("R21").Value
Sheets("Data Input").Range("B1310").Value = WorksheetFunction.Match(Sheets("Caledonian Road Fam Form").Range("O21").Value, Sheets("Data Input").Range("B1:B1000"), 0)
FTW = Sheets("Data Input").Range("B1310").Value
Sheets("Data Input").Cells(FTW, 25) = myVariable
MsgBox "The First page only will now print out for you."
InputStyleClear
是否有另一种我不知道的方法可以使例程锁定工作簿,因为它在手动解锁时工作正常。
这是 InputStyleClear 子例程
Sub InputStyleClear()
'Prevent Computer Screen from running
Application.ScreenUpdating = False
With ActiveSheet.Name
.Unprotect Password:="Pampigny", _
DrawingObjects:=False, Contents:=False, _
Scenarios:=False, UserInterFaceOnly:=False
End With
With ActiveSheet.Styles("Input")
.Interior.Pattern = xlNone
.Font.ColorIndex = xlAutomatic
.Borders(xlLeft).LineStyle = xlNone
.Borders(xlRight).LineStyle = xlNone
.Borders(xlTop).LineStyle = xlNone
.Borders(xlBottom).LineStyle = xlNone
End With
With ActiveSheet.Name
.Protect Password:="Pampigny", _
DrawingObjects:=True, Contents:=True, _
Scenarios:=True, UserInterFaceOnly:=True
End With
'Allow Computer Screen to refresh (not necessary in most cases)
Application.ScreenUpdating = True
End Sub
解决方案
编辑:此解决方案解释了工作簿样式的结构和行为,以及为什么需要取消保护所有工作表以修改任何样式。但是,为了避免需要不断地修改工作簿样式,我建议创建两个样式,例如InputOn
& InputOff
,并根据需要应用它们。这将消除修改样式的需要以及取消保护和保护所有工作表的要求。
需要强调的几点:
提及
Workbook
保护,但发布的代码不包含任何实例,Workbook.Protect
而应用的保护仅影响Worksheets
该
Workbook_Open
事件尝试保护所有应用该UserInterFaceOnly
属性的工作表,但它尝试将此属性应用于worksheets
已受保护的工作表,因此无法激活该属性。Worksheets
必须首先不受保护,然后保护包括财产才能UserInterFaceOnly=TRUE
生效。Style 对象 (Excel)是的成员,
Styles object
虽然位于Workbook
级别,但一旦 aWorksheet
受到保护,整个Styles collection
对象也将受到保护。因此,即使要修改Style
的 没有在任何.Worksheets
Style
Worksheets
建议将这些更改应用于您的程序:
Private Sub Workbook_Open()
Rem Using Sheet instead of Worksheet to care for Charts in the workbook if any
Dim Sht As Object
With ThisWorkbook
For Each Sht In .Sheets
With Sht
.Unprotect Password:="Password"
.Protect Password:="Password", _
DrawingObjects:=True, Contents:=True, _
Scenarios:=True, UserInterFaceOnly:=True
End With: Next
With .Sheets("Menu")
Activate
Application.Goto .Cells(1), 1
End With: End With
End Sub
在过程中PartialPrintFamForm
替换这些行:
ActiveSheet.Unprotect Password:="Password"
InputStyleRestore
用这些:
With ThisWorkbook
For Each Sht In .Sheets
Sht.Unprotect Password:="Password"
Next: End With
InputStyleRestore
With ThisWorkbook
For Each Sht In .Sheets
With Sht
.Protect Password:="Password", _
DrawingObjects:=True, Contents:=True, _
Scenarios:=True, UserInterFaceOnly:=True
End With: Next: End With
推荐阅读
- javascript - React:避免在路由上重新渲染相同的组件
- reactjs - 如何通过循环遍历reactjs中的数组来渲染组件?
- botframework - 为 Skype 注册新机器人
- ios - 使用 Swift 5 的蒸汽安装问题(第一次使用蒸汽)
- http - 无法使用 HTTPS 与智能物联网设备通信
- r - 检查特定列中的所有行元素是否为 NA
- raku - 如何为返回整个结构的 C 函数编写 Raku 声明?
- android - 在 BottomSheetBehavior 外部单击时,在 BottomSheetBehavior 外部拦截单击处理程序
- java - 如何在不转换为科学计数法的情况下在 csv 中写入冗长的数字
- bartender - 调酒师错误:“Web 服务”需要“企业自动化”版本或更高版本才能运行