简体   繁体   English

在A列中选择下一个非空单元格 - Excel VBA

[英]Select next non empty cell in A column - Excel VBA

Trying to have a button to select next non empty/blank cell in column A, relative to current row.试图有一个按钮来选择 A 列中下一个非空/空白单元格,相对于当前行。 The code below works, but only if active cell is in column A. Need it to work on column A, even when active cell is in another column.下面的代码有效,但仅当活动单元格在 A 列中时。需要它在 A 列上工作,即使活动单元格在另一列中也是如此。

Private Sub CommandButton3_Click() 'Next step button - selects next step in A column
Dim n As Long
n = Cells(Rows.Count, ActiveCell.Column).End(xlUp).Row
If ActiveCell.Row = n Then MsgBox "Last one, no more instructions.": Exit Sub

If ActiveCell.Offset(1, 0) = "" Then
    ActiveCell.End(xlDown).Select
Else
    ActiveCell.Offset(1, 0).Select
End If

End Sub

The problem is your activecell is in the wrong column.问题是您的活动单元格在错误的列中。 You need to address that:你需要解决这个问题:

Private Sub CommandButton3_Click() 'Next step button - selects next step in A column
Dim n As Long, fixed_range As Range
Set fixed_range = ActiveSheet.Cells(ActiveCell.Row, 1)
n = Cells(Rows.Count, fixed_range.Column).End(xlUp).Row
If fixed_range.Row = n Then MsgBox "Last one, no more instructions.": Exit Sub

If fixed_range.Offset(1, 0) = "" Then
    fixed_range.End(xlDown).Select
Else
    fixed_range.Offset(1, 0).Select
End If
End Sub

This uses a "fixed_range" to make sure we're working on column 1.这使用“fixed_range”来确保我们正在处理第 1 列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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