基于ActiveCell的选择

时间:2019-06-29 13:14:15

标签: excel vba

AH的数据之间有一些空白。我想在A列中找到“ ABC”,然后在上方选择2行-这就是我的ActiveCell

我要删除ActiveCellRow2之间的行(活动单元是动态的)

Sub format()   
    Cells.Find(What:="abc", _
               After:=ActiveCell, _
               LookIn:=xlFormulas, _
               LookAt:= xlPart, _
               SearchOrder:=xlByRows, _
               SearchDirection:=xlNext, _
               MatchCase:=False, _
               SearchFormat:=False).Activate
    ActiveCell.Offset(-2, 0).Select
    Range(Selection, ActiveCell, A2).Select   
End Sub

2 个答案:

答案 0 :(得分:1)

代码将为您完成这项工作:

Sub format()

Dim rng As Range

Set rng = Cells.Find(What:="abc", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
    xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
    , SearchFormat:=False)

    rng.Offset(-2, 0).Select

    Range(Cells(Selection.Row, 1), Cells(2, 1)).Select
    'Selection.EntireRow.Delete

End Sub

当前,我已注释掉最后一行,该行将删除所需的行。取消注释,但首先请确保这是您要删除的内容。

答案 1 :(得分:0)

对于Range,请尝试:

 (ActiveCell, "A2").Select