热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

删除单元格上的行范围单击-DeleteRangeofRowsonCellClick

Iwouldlikeamacro,thatrunswhenacellwiththetextvalueDELETEisclicked,anddeletesent

I would like a macro, that runs when a cell with the text value "DELETE" is clicked, and deletes entire the range of rows from one cell above and 19 cells below the cell with the value "DELETE".

我想要一个宏,它在单击文本值为“DELETE”的单元格时运行,并从单元格下方的单元格和“DELETE”下面的19个单元格中删除整个行范围。

For example if cell X7 has the value "DELETE", when it is clicked rows 6:26 are deleted.

例如,如果单元格X7的值为“DELETE”,则单击它时将删除行6:26。

The code I have so far is:

我到目前为止的代码是:

Private Sub Delete_Type(ByVal Target As Excel.Range)

    If Target.Address = "$X$7" Then
        Rows("(Row(), Column(),1, 4):((Row(), Column(), 4)+19)").Select
        Selection.delete Shift:=xlUp
    End If

End Sub

1 个解决方案

#1


0  

Try this one:

试试这个:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim firstRow As Long, lastRow As Long

    Application.EnableEvents = False
    On Error GoTo ErrorHandler


    If UCase(Target.Value) = "DELETE" Then
        'if target cell is in first row of sheet, we can't get row above
        firstRow = Application.Max(Target.Row - 1, 1)
        'if target cell is in last 19 rows of sheet, we can't get 19 rows below
        lastRow = Application.Min(Target.Row + 19, Rows.Count)

        Range(firstRow & ":" & lastRow).Delete Shift:=xlUp
    End If

ExitHere:
    Cancel = True
    Application.EnableEvents = True
    Exit Sub
ErrorHandler:
    Resume ExitHere
End Sub

As name of event Worksheet_BeforeDoubleClick suggests, it fires every time before double click on any cell.

作为事件名称Worksheet_BeforeDoubleClick建议,每次双击任何单元格之前它都会触发。

Put above code in Sheet module:

将以上代码放在Sheet模块中:

enter image description here


推荐阅读
author-avatar
手机用户2502899267
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有