防止用户根据该行中单元格的内容删除某些行

发布于 2024-12-10 02:05:50 字数 840 浏览 0 评论 0原文

我有一个想要保护的模板文件,以便用户无法修改公式。由于工作表受到保护,我编写了一个宏来允许用户插入行。我还想要一个宏来允许用户删除行,但我想防止用户删除某些关键行(例如检查总计和标题等)。

为此,我在模板中使用 L 列来标识无法删除的行。对于这些行,我在 L 列的该行中有单词 “keep”。我在下面编写了一个基本的删除宏,但我需要修改它以在所选范围 的 L 列中查找rRangeExit 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清浅ˋ旧时光 2024-12-17 02:05:50

这可能不是最好的方法,但它如下。我没有在最后的 if then else 中添加删除部分,因为我认为你可以处理这个问题

Sub DeteteRows()
Dim rRange As Range
Dim bKeepFound As Boolean
bKeepFound = False
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
        'dont need the else statement cause you exit the sub if it fails
    End If

    For Each Row In rRange.Rows
    Dim s 'variable to hold the array
    s = Split(Row.Address, ":") 'split out the column and row
        'remove the $ and convert to a number then check the cell value
        If rRange.Cells(CInt(Replace(s(0), "$", "")), 12).Value = "keep" Then
            bKeepFound = True
        End If
    Next Row
    'check to see if a row was found to keep
    If bKeepFound Then
        Exit Sub 'row was found so exit sub
    Else
        'delete the rows in the range
    End If

End Sub

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

Sub DeteteRows()
Dim rRange As Range
Dim bKeepFound As Boolean
bKeepFound = False
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
        'dont need the else statement cause you exit the sub if it fails
    End If

    For Each Row In rRange.Rows
    Dim s 'variable to hold the array
    s = Split(Row.Address, ":") 'split out the column and row
        'remove the $ and convert to a number then check the cell value
        If rRange.Cells(CInt(Replace(s(0), "$", "")), 12).Value = "keep" Then
            bKeepFound = True
        End If
    Next Row
    'check to see if a row was found to keep
    If bKeepFound Then
        Exit Sub 'row was found so exit sub
    Else
        'delete the rows in the range
    End If

End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文