从某个范围内的activecell中查找空单元格

时间:2016-11-28 11:47:42

标签: excel-vba vba excel

这里的新手需要帮助才能找到activecell.row ....范围内的下一个空单元格。

<div id="test">
  <div id="block1">
    <div>test</div>
    <div>test</div>
    <div>test</div>
    <div>test</div>
    <div>test</div>
    <div>test</div>
    <div>test</div>
    <div>test</div>
    <div>test</div>
    <div>test</div>
  </div>
  <div id="block2">
    sample
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

使用Find方法时,最好将结果设置为Range

当然,有可能Find不会返回任何结果(如果在指定的范围内找不到另一个空单元格),这就是我们添加If Not EmptyRng Is Nothing Then的原因。

<强>代码

Option Explicit

Sub FindNextCell()

Dim FindRng As Range, EmptyRng As Range

' define the Range to search according to ActiveCell current row
Set FindRng = Range("F" & ActiveCell.Row & ":I" & ActiveCell.Row)

' COMMENT : need to cheat a little to get the first cell founds in the searched range
'           by starting from the last column in the range, Column "I"
'           Otherwise, will return the second cell found in the searched range
Set EmptyRng = FindRng.Find(What:="", after:=Cells(ActiveCell.Row, "I"), _
                            LookIn:=xlValues, lookat:=xlWhole)

' found an empty cell in the specified range
If Not EmptyRng Is Nothing Then
    EmptyRng.Select
Else ' unable to find an empty cell in the specified range
    MsgBox "Unable to find an empty cell in " & FindRng.Address & " Range"
End If

End Sub