行中的第一个非空单元格 Excel VBA

First non empty cell in row Excel VBA

我正在尝试使用 VBA .Find 函数在一行中查找第一个非空单元格。代码如下:

    Sub test()

    With Selection
    Set firstNE = .Find(what:="*", LookIn:=xlValues)
    End With
    Debug.Print firstNE.Address

    End Sub

我遇到的问题是当我 select 范围时B4:F4 并且该范围内的所有单元格都非空 returns C4 而不是 B4。

当 B4 非空时也会发生同样的情况,然后下一个非空是例如E4,它returnsE4而不是B4。

只有 returns B4 如果除 B4 之外的所有 selection 都是空的。

谁能给我解释一下?

顺便说一句。我不想使用 IsEmpty+loop,因为它会花费太多时间来处理我的数据。

尝试提供 After 参数:

Sub test()

    With Selection
       Set firstNE = .Find(what:="*", LookIn:=xlValues, _
                           After:=.Cells(.Cells.count))
    End With

    If Not firstNE Is Nothing Then Debug.Print firstNE.Address

End Sub