简体   繁体   English

用户尝试删除行时的MsgBox

[英]MsgBox when user tries to delete a row

I am building a tool in excel and have protected the workbook so that deleting or inserting rows/columns is not allowed, using the code below: 我正在使用Excel构建工具并保护工作簿,以便不允许删除或插入行/列,使用以下代码:

Sub ProtectSheet()
ActiveSheet.Protect _
    DrawingObjects:=False, _
    Contents:=True, _
    Scenarios:=False, _
    AllowFormattingCells:=True, _
    AllowFormattingColumns:=True, _
    AllowFormattingRows:=True, _
    AllowInsertingHyperlinks:=True, _
    AllowInsertingColumns:=False, _
    AllowInsertingRows:=False, _
    AllowDeletingColumns:=False, _
    AllowDeletingRows:=False, _
    AllowSorting:=True, _
    AllowFiltering:=True, _
    AllowUsingPivotTables:=True, _
    Password:=cPassword
End Sub

However, I also want a message box to appear if the user tries to delete or insert a row/column that tells them they cannot do this and provides other instructions. 但是,如果用户尝试删除或插入一个告诉他们不能执行此操作并提供其他说明的行/列,我还希望显示一个消息框。 Can anyone advise me on how to do this? 任何人都可以告诉我如何做到这一点?

Thanks! 谢谢!

You can use the selection change event like this (code to go in the worksheet's code module): 你可以像这样使用选择更改事件(代码进入工作表的代码模块):

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    With Target
        If .Address = .EntireRow.Address Or .Address = .EntireColumn.Address Then
            MsgBox "You can not insert or delete any row or column", vbInformation, "FYI"
        End If
    End With
End Sub

If they try to insert or delete by selecting entire rows or columns (which is how I typically go about doing that) then this message box will pop up, but it won't pop up if you select smaller ranges of cells. 如果他们尝试通过选择整行或列来插入或删除(这就是我通常会这样做的话),则会弹出此消息框,但如果选择较小的单元格范围,则不会弹出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM