找到价值;如果找到,则移至下一行并删除所有空行,直到找到下一个非空行

时间:2017-03-03 14:21:46

标签: excel vba excel-vba

我有一个像这样的电子表格:

A栏

Blank
Blank
Something
Fax Number:
Blank
Blank 
Blank
Next non empty row

我使用以下VBA代码查找我的价值'传真号码:'在A栏:

Dim Rng As Range

Set Rng = Range("A:A")
Rng.Select
Set cell = Selection.Find(What:="Fax Number:", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)

If cell Is Nothing Then
    'do it something
Else
    Dim MyRange As Object
    Set MyRange = cell.Offset(1, 0).End(xlDown).Row
    MyRange.EntireRow.Delete
End If

如果找到,则应删除传真号码下面的所有空行,直到下一个非空行。

给出这个结果:

Column A
Blank
Blank
Something
Fax Number:
Next non empty row

有人能告诉我出错的地方吗?我得到一个未定义错误的对象:
Set MyRange = cell.Offset(1, 0).End(xlDown).Row

1 个答案:

答案 0 :(得分:0)

尝试使用以下代码,而不使用SelectionOption Explicit Sub UseFind() Dim Rng As Range, C As Range Set Rng = Range("A:A") Set C = Rng.Find(What:="Fax Number:", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False) If C Is Nothing Then 'do it something Else Dim LastRow As Long Dim MyRange As Range LastRow = C.Offset(1, 0).End(xlDown).Row - 1 '<-- get last row of Blank Set MyRange = Range(C.Offset(1, 0), Cells(LastRow, C.Column)) '<-- set the deleted range MyRange.EntireRow.Delete End If End Sub

'\n'
相关问题