选择范围取决于两个关键词

问题描述 投票:0回答:1

我目前工作的一个项目,以创建一个VBA自动删除空行。我已创建了一些帮助如下:

    Dim rngFirst As Range
    Dim rngLast As Range

    Application.ScreenUpdating = False

    With Sheets("Input")
        'Find the start and stop
        Set rngFirst = .Cells.Find(what:="[Performance Income]", lookat:=xlWhole, _
            LookIn:=xlValues, MatchCase:=False)
        Set rngLast = .Cells.Find(what:="[Miscellaneous Income]", _
            lookat:=xlWhole, LookIn:=xlValues, MatchCase:=False)

        .Range(rngFirst, rngLast).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    End With

    Application.ScreenUpdating = True

我有一个1004运行时错误。我已经寻找了原因,我认为这是与我的选择范围。但是我不知道具体的,我希望能得到帮助。先感谢您。

excel vba
1个回答
0
投票

这是为我工作。

你必须确保第一范围是最后一个前一个单元格,它们都位于同一列。否则,它抛出一个错误。

要定义新的范围,使用开始的地址并且完成范围。

Sub DeleteBlankCells()

    Dim rngFirst As Range
    Dim rngLast As Range

    Dim rngUnion As Range
    Application.ScreenUpdating = False

    With Sheets("Input")
        'Find the start and stop
        Set rngFirst = .Cells.Find(what:="Performance Income", lookat:=xlWhole, _
            LookIn:=xlValues, MatchCase:=False)
        Set rngLast = .Cells.Find(what:="Miscellaneous Income", _
            lookat:=xlWhole, LookIn:=xlValues, MatchCase:=False)

        Set rngUnion = Range(rngFirst.Address, rngLast.Address)

        rngUnion.SpecialCells(xlCellTypeBlanks).EntireRow.Delete

    End With

    Application.ScreenUpdating = True

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.