首页 > 解决方案 > 删除特定单元格正下方的单元格

问题描述

我正在编写一个脚本来查找一个值并删除该单元格及其正下方的单元格。我最初的想法是使用偏移量,但不确定如何使用。

Dim batch As Range

For Each batch In ActiveSheet.UsedRange
    If batch.Value = "Batch" Then batch.Cells.Delete Shift:=xlShiftUp
    'Delete cell below all cells containing 'batch'
Next batch

谢谢!

标签: excelvba

解决方案


利用Resize()

Sub lskdhfdg()
    Dim batch As Range
    
    For Each batch In ActiveSheet.UsedRange
        If batch.Value = "Batch" Then batch.Resize(2, 1).Delete Shift:=xlShiftUp
    Next batch
End Sub

编辑#1

只有 1 行更改:

Sub lskdhfdg2()
    Dim batch As Range
    
    For Each batch In ActiveSheet.UsedRange
        If InStr(batch.Value, "Batch") > 0 Then batch.Resize(2, 1).Delete Shift:=xlShiftUp
    Next batch
End Sub

推荐阅读