excel - Worksheet.Protect AllowDeletingRows 不允许删除行
问题描述
在我的工作簿中,我使用以下代码保护 Workbook_Open 上的所有工作表:
ws.Protect Password:="password", UserInterFaceOnly:=True, _
AllowFormattingColumns:=True, AllowFormattingRows:=True, _
AllowInsertingColumns:=True, AllowInsertingRows:=True, _
AllowDeletingColumns:=True, AllowDeletingRows:=True
在最后一行中,我专门打开 AllowDeletingRows 以允许用户删除行。插入行工作得很好,但如果我尝试删除受保护工作表上的一行 Excel 告诉我我不能删除包含锁定单元格的行。有谁知道如何让 AllowDeletingRows 正常工作?
解决方案
阅读微软文档:
[...]当工作表受到保护时,必须解锁包含要删除的单元格的行。
这意味着当受到保护时,如果要删除它们Sheet
,则必须先解锁。Rows
例子
在此示例中,您可以通过解锁来删除Row
第一行(在第一行之后)或整个Sheet
(在第二行之后)。
Sub ProtectionOptions()
'Unlock only row 1.
Rows("1:1").Locked = False
'Or Unlock all cells.
Cells.Locked = False
End Sub
编辑
这Sub
允许您询问用户要删除的 Row 并且您可以设置AllowDeletingRows:=False
,因为您不会使用 ExcelDelete
功能。这是代码:
Sub Setup()
ws.Protect Password:="password", UserInterFaceOnly:=True, _
AllowFormattingColumns:=True, AllowFormattingRows:=True, _
AllowInsertingColumns:=True, AllowInsertingRows:=True, _
AllowDeletingColumns:=True, AllowDeletingRows:=False
End Sub
'Connect this Sub to a button.
Sub DeleteRow()
Dim userInput As String
Dim row As Long
userInput = InputBox("Please enter the row number you want to delete:", "Delete Row")
If Not IsNumeric(userInput) Then
MsgBox "Provided input is not numeric. Please insert a valid row number", vbExclamation
Exit Sub
End If
row = userInput
ws.rows(row).Locked = False
ws.rows(row).Delete
End Sub
注意:此代码经过测试并且可以正常工作。
希望这可以帮助。
推荐阅读
- reactjs - 有没有办法访问谷歌照片所做的照片分类?或者最近浏览过的照片?
- yii2 - 调用未知方法:yii\db\Command::select()
- ruby-on-rails - 使用多态关联来确定所有权?或者我应该在桌子上留下一个空列?
- vue.js - 如何使用与普通路由不同的模板处理 vue-router 的 404 错误
- javascript - 在我的主要组件中嵌套路由的问题
- java - 我无法处理 A 片段中的 RecyclerView onClick 事件
- html - 将图像高度放大到最大高度的最佳方法是什么?
- c++ - 映射插入(const_iterator 提示,值)用法
- python - Scrapy 推送到 Heroku 得到错误:推送被拒绝,无法编译 Python 应用程序
- powershell - 我想通过检查嵌套文件夹将具有选定扩展名的文件移动到另一个文件夹中。谁能帮助我