Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 当用户尝试删除行时,MsgBox_Vba_Excel_Rowdeleting - Fatal编程技术网

Vba 当用户尝试删除行时,MsgBox

Vba 当用户尝试删除行时,MsgBox,vba,excel,rowdeleting,Vba,Excel,Rowdeleting,我正在excel中构建一个工具,并已使用以下代码保护工作簿,因此不允许删除或插入行/列: Sub ProtectSheet() ActiveSheet.Protect _ DrawingObjects:=False, _ Contents:=True, _ Scenarios:=False, _ AllowFormattingCells:=True, _ AllowFormattingColumns:=True, _ AllowFormatting

我正在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
但是,如果用户试图删除或插入一行/列,告诉他们不能这样做,并提供其他说明,我还希望出现一个消息框。有谁能告诉我怎么做吗


谢谢

您可以像这样使用选择更改事件(工作表代码模块中的代码):


如果他们试图通过选择整行或整列来插入或删除(我通常就是这样做的),则会弹出此消息框,但如果选择较小范围的单元格,则不会弹出此消息框。

您可以使用如下选择更改事件(工作表代码模块中的代码):


如果他们试图通过选择整行或整列来插入或删除(这是我通常的做法),则会弹出此消息框,但如果选择较小范围的单元格,则不会弹出此消息框。

在代码运行之前使用
MsgBox
如何?在代码运行之前使用
MsgBox
如何,非常感谢!这正是我需要的。哇,非常感谢!这正是我所需要的。
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