防止用户根据该行中单元格的内容删除某些行
我有一个想要保护的模板文件,以便用户无法修改公式。由于工作表受到保护,我编写了一个宏来允许用户插入行。我还想要一个宏来允许用户删除行,但我想防止用户删除某些关键行(例如检查总计和标题等)。
为此,我在模板中使用 L 列来标识无法删除的行。对于这些行,我在 L 列的该行中有单词 “keep”
。我在下面编写了一个基本的删除宏,但我需要修改它以在所选范围 的 L 列中查找rRange
和 Exit Sub
(如果有“keep”
一词)。
*请注意,rRange
可能包含许多相邻行,因此如果其中任何行未通过测试,宏将需要退出。
Sub DeteteRows()
Dim rRange As Range
On Error Resume Next
Application.DisplayAlerts = False
Set rRange = Application.InputBox(Prompt:= _
"Please use mouse to select a row to Delete.", _
Title:="SPECIFY ROW TO DELETE", Type:=8)
On Error GoTo 0
Application.DisplayAlerts = True
If rRange Is Nothing Then
Exit Sub
Else
rRange.EntireRow.Delete
Range("a1").Select
MsgBox ("Row(s) Deteted")
End If
End Sub
I have a template file that I want to protect so that users cannot modify formulas. As the sheet is protected, I have written a macro to allow the user to insert rows. I also want a macro to allow the user to delete rows, but I want to prevent the user from deleting certain critical rows (e.g. check totals and headings, etc.).
To this end I have used column L in my template to identify rows that cannot be deleted. For these rows I have the word "keep"
in that row of column L. I have written a basic delete macro below but I need to modify it to look in column L of the selected range rRange
and Exit Sub
if the word "keep"
is there.
*Note that rRange
could contain a number of adjacent rows so the macro would need to exit if any of those rows fail the test.
Sub DeteteRows()
Dim rRange As Range
On Error Resume Next
Application.DisplayAlerts = False
Set rRange = Application.InputBox(Prompt:= _
"Please use mouse to select a row to Delete.", _
Title:="SPECIFY ROW TO DELETE", Type:=8)
On Error GoTo 0
Application.DisplayAlerts = True
If rRange Is Nothing Then
Exit Sub
Else
rRange.EntireRow.Delete
Range("a1").Select
MsgBox ("Row(s) Deteted")
End If
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能不是最好的方法,但它如下。我没有在最后的 if then else 中添加删除部分,因为我认为你可以处理这个问题
This may not be the best way but it is below. I did not add the delete portion in the last if then else as I figured you can handle that