单元格填充后如何选择下一个单元格

时间:2020-03-26 11:07:17

标签: excel vba

我想指定我的擅长领域。我的意思是什么时候第一个单元格应该是S5,然后填充到C6,然后到C11,E11,G11,I11,K11,M11,O11,Q11,S11,然后到C12 E12 ... same方法直到s34,然后转到H36。

我正在尝试使用它:

If Not IsEmpty(Range("$C$11:$Q$11").Value) Then ActiveCell.Offset(0, 1).Select

不幸的是,在这种情况下,活动单元格向下移动一行,向右移动一行,并且不仅在指定范围内使用。我的Excel宏不好。

3 个答案:

答案 0 :(得分:1)

运行此代码,看看它能做什么。

我不确定您到底在做什么,这只是以单元格地址为例。

Sub x()

Dim cl As Range, r As Long

For r = 11 To 12
    For Each cl In Range("C" & r & ", E" & r & ",G" & r)
        MsgBox cl.Address
    Next cl
Next r

End Sub

答案 1 :(得分:0)

看看worksheet_change事件。编辑单元格时,您可以检查要退出的单元格并强制移动到所需的单元格。

答案 2 :(得分:0)

大体上,下面的代码可以实现您所描述的。请将其安装在要对其执行操作的工作表的代码表上(不是标准代码模块)。正确的位置对其功能至关重要。

Private Sub Worksheet_Activate()
    Cells(5, "S").Select
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim Rng As Range
    Dim C As Long
    Dim R As Long

    If Target.Cells.CountLarge > 1 Then Exit Sub

    Set Rng = Application.Union(Range("S5"), Range("C6"))
    For C = 5 To 19 Step 2
        Set Rng = Application.Union(Rng, Range(Cells(11, C), Cells(34, C)))
    Next C

    If Not Application.Intersect(Target, Rng) Is Nothing Then
        Select Case Target.Row
            Case 5
                Rng.Areas(2).Select
            Case 6
                With Rng.Areas(1)
                    If Len(.Value) = 0 Then
                        GoBack .Row, .Column
                    Else
                        Rng.Areas(3).Cells(1).Select
                    End If
                End With
            Case Else
                C = Rng.Areas.Count
                With Rng.Areas(C)
                    If Target.Address = .Cells(.Cells.Count).Address Then
                        Cells(36, "H").Select
                    Else
                        With Target
                            R = .Row
                            C = .Column + 2
                        End With
                        If C > .Column Then
                            R = R + 1
                            C = Rng.Areas(3).Column
                        End If
                        Cells(R, C).Select
                    End If
                End With
        End Select
    End If
End Sub

Private Sub GoBack(R As Long, _
                   C As Long)

    Dim Cell As Range

    Set Cell = Cells(R, C)
    MsgBox "Cell " & Cell.Address(0, 0) & " must be filled first.", _
           vbExclamation, "Missing data"
    Cell.Select
End Sub

我已对其进行编程,因此只要激活工作表,就选择S5。用户对其进行更改后,将选择C6。如果更改了C6,代码将检查S5是否已填充,并指示用户是否仍为空白。该方法可以扩展为涵盖是否必须填写所有单元的完整检查。由于现在是代码,因此进行更改后,选择将移至下一个单元格,而在填充S34之后将移至H36。

相关问题