实现下一个按钮在空白时跳过行

时间:2011-10-31 04:23:44

标签: excel vba

您好我试图实现代码点击下一步。我见过的大多数例子似乎只是递增到下一行。我在A列中有连续的单元格,但是有些单元格是空的,我希望它用数据而不是下一行跳到下一行。

我正在使用这个示例代码但是对于我的生活似乎可以让它增加并跳过空白

Private Sub cmdNext_Click()
    ' Increment row number:
    lCurrentRow = lCurrentRow + 1

    ' Show contents of new row in the form:
    txtName.Text = Cells(lCurrentRow, 1).Value
    txtPhone.Text = Cells(lCurrentRow, 2).Value
End Sub

2 个答案:

答案 0 :(得分:2)

试试这个:

Private Sub cmdNext_Click()
    Dim rFound As Range

    Set rFound = Columns(1).Find(What:="*", After:=Cells(lCurrentRow, 1))

    If Not rFound Is Nothing Then
        If rFound.Row > lCurrentRow Then
            lCurrentRow = rFound.Row
            txtName.Text = Cells(lCurrentRow, 1).Value
            txtPhone.Text = Cells(lCurrentRow, 2).Value
        End If
    End If

    Set rFound = Nothing

End Sub

答案 1 :(得分:0)

Private Sub cmdNext_Click()
    ' Increment row number:
    lCurrentRow = lCurrentRow + 1

    do while Len(Cells(lCurrentRow, 1).Value) = 0
       LCurrentRow = lCurrentRow + 1
    Loop

    ' Show contents of new row in the form:
    txtName.Text = Cells(lCurrentRow, 1).Value
    txtPhone.Text = Cells(lCurrentRow, 2).Value
End Sub

你需要一些东西来阻止它在表格的末尾运行......