简体   繁体   中英

Find Empty Cell from activecell in a range

Newbie here need help to find the next emptycell in a range from the activecell.row....

Sub FindNextCell()
'
'Macro1 Macro

Cells.Find(What:="", _
             After:=Range("F2:I22")(Cells(ActiveCell.Row, _
             Columns.Count).End(xlToLeft).Offset(0, 1)), _
             LookIn:=xlFormulas, LookAt:=xlPart, _
             SearchOrder:=xlByRows, _
             SearchDirection:=xlNext, _
             MatchCase:=False, _
             SearchFormat:=False).Activate
End Sub

When using the Find method, it's better to set the result as a Range .

Of course, there is a possibilty the Find will not return any results (in case it doesn't find another empty cell in the specifed range), that's why we add If Not EmptyRng Is Nothing Then .

Code

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM