密码仅保护 Excel VBA 中的部分工作表

Password only protects some sheets in Excel VBA

所以我一直在尝试包含一个代码来保护我作品sheet中的所有作品sheet,使用VBA,每个sheet具有不同的属性。我一直在玩弄以下代码,但出于某种原因,它只使用密码保护一些 sheets 而不是其他人。请帮忙。

Sub ProtegerTodo()



Sheets("Sheet1").Protect Password:="Password", _
    UserInterfaceOnly:=True, _
    DrawingObjects:=False, _
    Contents:=True, _
    Scenarios:=False, _
    AllowFormattingCells:=False, _
    AllowFormattingColumns:=False, _
    AllowFormattingRows:=False, _
    AllowInsertingColumns:=False, _
    AllowInsertingRows:=False, _
    AllowInsertingHyperlinks:=False, _
    AllowDeletingColumns:=False, _
    AllowDeletingRows:=False, _
    AllowSorting:=False, _
    AllowFiltering:=True, _
    AllowUsingPivotTables:=False

'Allowing selection of locked or unlocked cells
Sheets("Sheet1").EnableSelection = xlNoRestrictions

'Allowing selection of unlocked cells only
'Sheets("Sheet1").EnableSelection = xlUnlockedCells

'Do not allow selection of any cells
'Sheets("Sheet1").EnableSelection = xlNoSelection

Sheets("Consecutivo de Cirugías").Protect Password:="Password", _
    UserInterfaceOnly:=True, _
    DrawingObjects:=False, _
    Contents:=True, _
    Scenarios:=False, _
    AllowFormattingCells:=False, _
    AllowFormattingColumns:=False, _
    AllowFormattingRows:=False, _
    AllowInsertingColumns:=False, _
    AllowInsertingRows:=False, _
    AllowInsertingHyperlinks:=False, _
    AllowDeletingColumns:=False, _
    AllowDeletingRows:=False, _
    AllowSorting:=False, _
    AllowFiltering:=True, _
    AllowUsingPivotTables:=False

'Allowing selection of locked or unlocked cells
Sheets("Consecutivo de Cirugías").EnableSelection = xlNoRestrictions

'Allowing selection of unlocked cells only
'Sheets("Sheet1").EnableSelection = xlUnlockedCells

'Do not allow selection of any cells
'Sheets("Sheet1").EnableSelection = xlNoSelection

Sheets("Nueva CX").Protect Password:="Password", _
    UserInterfaceOnly:=True, _
    DrawingObjects:=False, _
    Contents:=True, _
    Scenarios:=False, _
    AllowFormattingCells:=True, _
    AllowFormattingColumns:=True, _
    AllowFormattingRows:=True, _
    AllowInsertingColumns:=False, _
    AllowInsertingRows:=False, _
    AllowInsertingHyperlinks:=False, _
    AllowDeletingColumns:=False, _
    AllowDeletingRows:=False, _
    AllowSorting:=False, _
    AllowFiltering:=True, _
    AllowUsingPivotTables:=False

'Allowing selection of locked or unlocked cells
'Sheets("Nueva CX").EnableSelection = xlNoRestrictions

'Allowing selection of unlocked cells only
Sheets("Nueva CX").EnableSelection = xlUnlockedCells

'Do not allow selection of any cells
'Sheets("Sheet1").EnableSelection = xlNoSelection

Sheets("Catalogo").Protect Password:="Password", _
    UserInterfaceOnly:=True, _
    DrawingObjects:=False, _
    Contents:=True, _
    Scenarios:=False, _
    AllowFormattingCells:=False, _
    AllowFormattingColumns:=False, _
    AllowFormattingRows:=False, _
    AllowInsertingColumns:=False, _
    AllowInsertingRows:=False, _
    AllowInsertingHyperlinks:=False, _
    AllowDeletingColumns:=False, _
    AllowDeletingRows:=False, _
    AllowSorting:=False, _
    AllowFiltering:=True, _
    AllowUsingPivotTables:=False

'Allowing selection of locked or unlocked cells
Sheets("Catalogo").EnableSelection = xlNoRestrictions

'Allowing selection of unlocked cells only
'Sheets("Sheet1").EnableSelection = xlUnlockedCells

'Do not allow selection of any cells
'Sheets("Sheet1").EnableSelection = xlNoSelection

Sheets("Procedimientos").Protect Password:="Password", _
    UserInterfaceOnly:=True, _
    DrawingObjects:=False, _
    Contents:=True, _
    Scenarios:=False, _
    AllowFormattingCells:=False, _
    AllowFormattingColumns:=False, _
    AllowFormattingRows:=False, _
    AllowInsertingColumns:=False, _
    AllowInsertingRows:=False, _
    AllowInsertingHyperlinks:=False, _
    AllowDeletingColumns:=False, _
    AllowDeletingRows:=False, _
    AllowSorting:=False, _
    AllowFiltering:=True, _
    AllowUsingPivotTables:=False

'Allowing selection of locked or unlocked cells
Sheets("Procedimientos").EnableSelection = xlNoRestrictions

'Allowing selection of unlocked cells only
'Sheets("Sheet1").EnableSelection = xlUnlockedCells

'Do not allow selection of any cells
'Sheets("Sheet1").EnableSelection = xlNoSelection

Sheets("Inventario").Protect Password:="Password", _
    UserInterfaceOnly:=True, _
    DrawingObjects:=False, _
    Contents:=True, _
    Scenarios:=False, _
    AllowFormattingCells:=False, _
    AllowFormattingColumns:=False, _
    AllowFormattingRows:=False, _
    AllowInsertingColumns:=False, _
    AllowInsertingRows:=False, _
    AllowInsertingHyperlinks:=False, _
    AllowDeletingColumns:=False, _
    AllowDeletingRows:=False, _
    AllowSorting:=False, _
    AllowFiltering:=True, _
    AllowUsingPivotTables:=False

'Allowing selection of locked or unlocked cells
Sheets("Inventario").EnableSelection = xlNoRestrictions

'Allowing selection of unlocked cells only
'Sheets("Sheet1").EnableSelection = xlUnlockedCells

'Do not allow selection of any cells
'Sheets("Sheet1").EnableSelection = xlNoSelection

Sheets("Ingreso de material").Protect Password:="Password", _
    UserInterfaceOnly:=True, _
    DrawingObjects:=False, _
    Contents:=True, _
    Scenarios:=False, _
    AllowFormattingCells:=True, _
    AllowFormattingColumns:=True, _
    AllowFormattingRows:=True, _
    AllowInsertingColumns:=False, _
    AllowInsertingRows:=False, _
    AllowInsertingHyperlinks:=False, _
    AllowDeletingColumns:=False, _
    AllowDeletingRows:=False, _
    AllowSorting:=False, _
    AllowFiltering:=True, _
    AllowUsingPivotTables:=False

'Allowing selection of locked or unlocked cells
Sheets("Ingreso de material").EnableSelection = xlNoRestrictions

'Allowing selection of unlocked cells only
'Sheets("Sheet1").EnableSelection = xlUnlockedCells

'Do not allow selection of any cells
'Sheets("Sheet1").EnableSelection = xlNoSelection

End Sub

注意:我知道它不是很有效,但它应该可以解决问题。

先谢谢你。

刚刚尝试了您的代码,它在每个 sheet 上都有效。 尝试一次删除一个 sheet 并删除其保护代码,这样您就会找到引发问题的确切 sheet。

然后只保留 sheet 并尝试使用代码进行保护。如果失败,则通过右键单击 sheet 名称进行保护,您将在 UI 中收到报告的问题 - 有时代码不报告错误。

如果有帮助请告诉我。