如果单元格已经填充,如何进行下一步? (用于Excecl(VBA)中数据输入的自动单元移动)

时间:2019-02-19 17:22:57

标签: excel vba

如果数组的宽度为11个单元格,深度为2行。在将以下代码重复一行之后,第二行的第3列将已经填充。

如果在第二行结束后我想在下一个2x11数组的第3列(不会填充)处重新启动,该如何跳至第二行的第5列?

使用以下代码:

Private Sub Worksheet_Change(ByVal Target As Range)

If Not Target.Cells.CountLarge > 1 Then
    If Not Intersect(Target, Columns(3)) Is Nothing Then
        Target.Offset(, 2).Select
    ElseIf Not Intersect(Target, Columns(5)) Is Nothing Then
        Target.Offset(, 1).Select
    ElseIf Not Intersect(Target, Columns(6)) Is Nothing Then
        Target.Offset(, 2).Select
    ElseIf Not Intersect(Target, Columns(8)) Is Nothing Then
        Target.Offset(, 2).Select
    ElseIf Not Intersect(Target, Columns(10)) Is Nothing Then
        Target.Offset(, 1).Select
    ElseIf Not Intersect(Target, Columns(11)) Is Nothing Then
        Target.Offset(, -2).Select
    ElseIf Not Intersect(Target, Columns(9)) Is Nothing Then
        Target.Offset(1, -6).Select
    End If
End If

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rw As Range

    If Not Target.Cells.CountLarge > 1 Then

        Set rw = Target.EntireRow

        Select Case Target.Column
            Case 3: rw.Cells(5).Select
            Case 5: rw.Cells(6).Select
            Case 6: rw.Cells(8).Select
            Case 8: rw.Cells(10).Select
            Case 10: rw.Cells(11).Select
            Case 11: rw.Cells(9).Select
            Case 9:
                'not exactly following your layout, so you may need to 
                '   adjust this test to fit your needs
                If rw.Row Mod 2 = 0 Then
                    rw.Cells(3).Offset(1, 0).Select
                Else
                    rw.Cells(5).Offset(1, 0).Select
                End If
        End Select
    End If

End Sub

它将检查一行是否为奇数/偶数,以确定在下一个输入行中从哪里开始