在选定的单元格之后转到第一个空单元格

我试图实现一个代码,如果你点击某个单元格; 你去某个列中的第一个空单元格。

现在我有这个代码:

If Selection.Count = 1 Then If Not Intersect(Target, Range("B2")) Is Nothing Then Columns("E").Find(vbNullString, Cells(Rows.Count, "E")).Select End If End If 

但是这个代码存在一个问题:我希望它开始检查第一个空单元; 从第3行开始。我该怎么做?

EDIT1:

我已经对代码进行了一些调整,以适应我的需求(对于实践和美学)。

 Dim lastCell As Range Set lastCell = Range("E:E").Find(vbNullString, [E3], , , , xlNext) lastCell.Interior.Color = RGB(100, 200, 100) lastCell.Offset(0, -3) = "Last Cell -->" lastCell.Offset(0, -3).Interior.Color = RGB(0, 110, 250) lastCell.Offset(0, -3).Font.Color = vbWhite If Not Intersect(Target, [B2]) Is Nothing Then lastCell.Select 

注意偏移三列到右边的原因是由于表格的布局:)如果lastCell改变,我清除单元格的格式和其他地方的文本。 所以如果有人有兴趣,让我知道。

您可以像这样重新编写代码,只需提供SearchDirection参数即可。

 Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Selection.Count = 1 Then If Not Intersect(Target, Range("B2")) Is Nothing Then Columns("E").Find(vbNullString, Cells(Rows.Count, "E") _ , , , , xlPrevious).Select End If End If End Sub 

或者你可以试试这个:

编辑1:对于brettdj 🙂

 Private Sub Worksheet_SelectionChange(ByVal Target As Range) On Error Goto errhandler Application.EnableEvents = False If Target.Cells.Count = 1 Then If Not Intersect(Target, [B2]) Is Nothing Then _ Range("E:E").Find(vbNullString, [E3], , , , xlNext).Select End If continue: Application.EnableEvents = True Exit Sub errhandler: MsgBox Err.Description Resume continue End Sub 

除E3:E(x)之间存在空白单元外,两个代码的工作方式都是相同的。
您的修订代码find列E中的第一个空单元格,并引用最后一个非空单元格。
下一个代码从字面上findE3的第一个空单元。 不知道你确实需要哪个。

附注:

Columns("E")Range("E:E")
为什么使用Range("E:E")呢? 那么, Intellisense感应与Range而不是与Columns踢。
所以我更喜欢使用Range所以你可以看到.Find方法的所有可用参数。

这是我会做的:

  Dim maxrows&, iRow&, iCol&, zcell As Range maxrows = Excel.Rows.Count If Selection.Count = 1 Then iRow = Target.Row iCol = Target.Column Set zcell = Range(Cells(3, iCol), Cells(maxrows, iCol)).Find(vbNullString) zcell.Select End If