回车时光标跳转到同行不同列,有其他特殊要求
Make cursor jump to same row, different column when pressing Enter, with other specific requirements
我查看了一下,虽然我发现了一些类似的线程(relevancy/usefulness 的程度不同),但其中 none 涵盖了我的确切情况。
我想在我的工作簿中使用一个 sheet 作为表单,在其中我在一列中输入一种类型的数据并按回车键,然后跳转到同一行的另一列以输入第二种类型数据。
我的 sheet 有一个“项目编号”列和一个“计数”列,它们之间有几列。
我想要实现的是:
- 在“项目编号”列的单元格中键入数据,然后按 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 中的活动单元格地址。这与其他一些代码一起工作,让我用颜色填充该工作中的活动单元格sheet,这样当传播sheet 变得繁忙时我可以看到更好的东西。
第二行(以“Cells”开头)是假设使光标跳转到“在“项目编号”列中按 Enter 后计数”列。
现在,除了一个主要缺点外,这一切都很好......
单击“项目编号”列中的单元格时,在我能够输入任何数据之前,光标立即移动到“计数”列,更不用说按回车键了。
我做错了什么?
和
我该如何确保 only 在按下 Enter after[= 时发生52=] 在“项目编号”列的 空白 单元格中输入数据?
我希望这是有道理的,阅读这么多关于代码的文章让我眼花缭乱。
干杯
请尝试 Change 事件。下面的代码应该做你想做的。它可以扩展以响应(不同地)其他列中的更改。
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()
事件并使用 sheet 作用域变量来检查正在更改的空单元格
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
我查看了一下,虽然我发现了一些类似的线程(relevancy/usefulness 的程度不同),但其中 none 涵盖了我的确切情况。
我想在我的工作簿中使用一个 sheet 作为表单,在其中我在一列中输入一种类型的数据并按回车键,然后跳转到同一行的另一列以输入第二种类型数据。
我的 sheet 有一个“项目编号”列和一个“计数”列,它们之间有几列。
我想要实现的是:
- 在“项目编号”列的单元格中键入数据,然后按 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 中的活动单元格地址。这与其他一些代码一起工作,让我用颜色填充该工作中的活动单元格sheet,这样当传播sheet 变得繁忙时我可以看到更好的东西。
第二行(以“Cells”开头)是假设使光标跳转到“在“项目编号”列中按 Enter 后计数”列。
现在,除了一个主要缺点外,这一切都很好...... 单击“项目编号”列中的单元格时,在我能够输入任何数据之前,光标立即移动到“计数”列,更不用说按回车键了。
我做错了什么? 和 我该如何确保 only 在按下 Enter after[= 时发生52=] 在“项目编号”列的 空白 单元格中输入数据?
我希望这是有道理的,阅读这么多关于代码的文章让我眼花缭乱。 干杯
请尝试 Change 事件。下面的代码应该做你想做的。它可以扩展以响应(不同地)其他列中的更改。
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()
事件并使用 sheet 作用域变量来检查正在更改的空单元格
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