繁体   English   中英

按回车键时使光标跳转到同一行、不同列,其他具体要求

[英]Make cursor jump to same row, different column when pressing Enter, with other specific requirements

我已经看过了,虽然我发现了一些类似的线程(具有不同程度的相关性/有用性),但它们都没有涵盖我的确切情况。

我想将工作簿中的工作表用作表单,在其中我在一列中输入一种类型的数据并按 Enter,然后跳转到同一行的另一列以输入第二种类型的数据。

我的工作表有一个“项目编号”列和一个“计数”列,它们之间有几列。

我想要实现的是:

  • 在“项目编号”列的单元格中键入数据,然后按 Enter。
  • 光标出现在同一行,“计数”列右侧的几列。

我希望这仅在在“项目编号”列中将数据输入到空白单元格按 Enter 时发生。 在任何其他列中按 Enter 应该只允许光标按其他方式运行。

目前我有代码

        Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Range("A100") = ActiveCell.Address

Cells(Application.ActiveCell.Row, 11).Select

End Sub

第一行(以“Range”开头)允许我查看单元格 A100 中的活动单元格地址。 这与其他一些代码一起使用,让我可以为该工作表中的活动单元格着色,以便在电子表格忙碌时可以更好地查看内容。

第二行(以“单元格”开头)是在“项目编号”列中按 Enter 后,应该使光标跳转到“计数”列的代码。

现在,除了一个主要缺点之外,这一切都很好......当单击“项目编号”列中的单元格时,在我能够输入任何数据之前,光标立即移动到“计数”列,更不用说按下 Enter 键了。

我究竟做错了什么? 以及如何确保在“项目编号”列的空白单元格中输入数据按 Enter 时才会发生这种情况?

我希望这是有道理的,阅读了这么多关于代码的内容让我眼睛一亮。 干杯

请尝试更改事件。 下面的代码应该做你想做的。 可以扩展它以(以不同方式)响应其他列中的更改。

Private Sub Worksheet_Change(ByVal Target As Range)

    Const TriggerColumn As Long = 3             ' modify as required
    Const NextColumn As Long = 11               ' modify as required

    Dim Rng As Range

    ' don't take action if more than 1 cell was changed
    If Target.Cells.CountLarge > 1 Then Exit Sub

    ' don't include the caption row in ther ange
    ' find the last used row in a column filled BEFORE Triggercolumn
    Set Rng = Range(Cells(2, TriggerColumn), Cells(Rows.Count, "A").End(xlUp))

    ' take action only if the changed cell is within Rng
    If Not Application.Intersect(Target, Rng) Is Nothing Then
        ' skip if the change resulted in a blank cell
        With Target
            If Len(.Value) Then
                Cells(.Row, NextColumn).Select
            Else
                ' prevent the next action from triggering this function
                Application.EnableEvents = False
                ' delete contents of the next cell
                Cells(.Row, NextColumn).ClearContents
                Application.EnableEvents = True
            End If
        End With
    End If
End Sub

您应该结合Worksheet_Change()Worksheet_SelectionChange()事件并使用Worksheet_SelectionChange()表范围的变量来检查正在更改的空单元格

Option Explicit

Dim emptyCell As Boolean

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub ' don't bother nulticell selections
    If Cells(1, Target.Column).Value <> "Item Number" Then Exit Sub ' don't bother selections outside "Item Number" column
    If emptyCell And Not IsEmpty(Target.Value) Then Cells(Target.Row, Range("A1", Cells(1, Columns.Count).End(xlToLeft)).Find(what:="Count", LookIn:=xlValues, lookat:=xlWhole).Column).Select ' if current cell was empty and now it is not then skip to "Count" column same row
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    emptyCell = IsEmpty(Target.Value) ' store the info about current selection being empty
End Sub

暂无
暂无

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

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