创建用于单击 VBA 上的按钮后移至下一行的代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23136929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:48:28  来源:igfitidea点击:

Creating code for Moving onto Next Row upon Clicking on Button on VBA

excelvbaexcel-vba

提问by TrevorDhien

I have a set of values in column B where B2 is selected automatically. I want to create a macro where upon clicking on Button1, the selection would move to cell B3; another click and B4 and so on and so forth each requiring a click on Button1 to move onto the next row down up until the current cell is empty.

我在 B 列中有一组值,其中 B2 是自动选择的。我想创建一个宏,点击 Button1 后,选择将移动到单元格 B3;再次单击和 B4 等等,每次都需要单击 Button1 才能向下移动到下一行,直到当前单元格为空。

Thanks much guys.. I'm still new to VBA

非常感谢你们..我还是 VBA 的新手

Private Sub CommandButton1_Click()
    Range("b2").Select
    For Each cell In Range("b:b")
        If cell.Value = "" Then Exit For
        cell.Offset(1, 0).Select
    Next
End Sub

回答by Alex P

How about:

怎么样:

Private Sub CommandButton1_Click()
    If Not Intersect(ActiveCell, Range("B:B")) Is Nothing Then
        If ActiveCell.Offset(1, 0) <> vbNullString Then
            ActiveCell.Offset(1, 0).Select
        End If
    End If
End Sub