简体   繁体   English

限制删除特定范围内的行

[英]Restrict deletion of rows in specific range

I have simple code for deleting rows by selecting specific cell.我有通过选择特定单元格来删除行的简单代码。 The problem is that It should not be allowed to delete rows in range A1:AZ7 I guess I need if statement for this operation?问题是它不应该被允许删除A1:AZ7范围内的行我想我需要这个操作的 if 语句? What it should be?它应该是什么?

Private Sub CommandButton24_Click()
    ThisWorkbook.Worksheets("GanttChart").Unprotect Password:="123456"

    Dim MyRange As Range
    Dim TestRange As Range

    Set TestRange = ThisWorkbook.Worksheets("GanttChart").Range("A1:AZ7")
    Set MyRange = ' slected cell

    If Not Application.Intersect(MyRange, TestRange) Is Nothing Then
        ThisWorkbook.Worksheets("GanttChart").Range("A" & ActiveCell.Row).Rows(1).EntireRow.Delete
    End If

    ThisWorkbook.Worksheets("GanttChart").Protect Password:="123456"
End Sub
Private Sub CommandButton24_Click()
    With ThisWorkbook.Worksheets("GanttChart")
        .Unprotect Password:="123456"

        Dim MyRange     As Range
        Dim TestRange   As Range

        Set TestRange = .Range("A1:AZ7")
        Set MyRange = Selection     ' U Can select many cells

        If Application.Intersect(MyRange.EntireRow.Rows, TestRange.EntireRow.Rows) Is Nothing Then
            'ThisWorkbook.Worksheets("GanttChart").Range("A" & ActiveCell.Row).Rows(1).EntireRow.Delete
            MyRange.EntireRow.Delete
        End If

        .Protect Password:="123456"
    End With
End Sub
    Private Sub CommandButton24_Click()
        ThisWorkbook.Worksheets("GanttChart").Unprotect Password:="123456"

        Dim MyRange As Range
        Dim TestRange As Range

        Set TestRange = ThisWorkbook.Worksheets("GanttChart").Range("A1:AZ7")
        Set MyRange = ActiveCell

        If Not Application.Intersect(MyRange, TestRange) Is Nothing Then
        Else
            ThisWorkbook.Worksheets("GanttChart").Range("A" & ActiveCell.Row).Rows(1).EntireRow.Delete
        End If

        ThisWorkbook.Worksheets("GanttChart").Protect Password:="123456"
    End Sub

At the moment you are checking if the ranges does not not intersect.目前,您正在检查范围是否不相交。 Try it again without the "not" in the if statement.再试一次,不要在 if 语句中加上“not”。 The not and is nothing are cancelling each other out. not 和 is nothing 相互抵消。

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

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