Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rngLastColumn As Range
'Note: The {Tab} or {Enter} key triggers selectionChange event.'
' Modify the address of rngLastColumn as needed. It should be one column beyond
' the last column of your inputs, so if input use columns A:F, then it should
' be Range("G:G").
Set rngLastColumn = Range("G:G")
If Not Intersect(Target, rngValidColumns.Columns(7)) Is Nothing Then
'Insert a new row:'
Target.EntireRow.Offset(1, 0).Insert
'Select the first cell in the new row'
cells(Target.Row + 1, 1).Select
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
'Do nothing if more than one cell is changed or content deleted
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
'Column F
If Target.Column = "6" Then
'Insert new row bellow
ActiveCell.EntireRow.Offset(1, 0).Insert
'Select first cell of next row just inserted
ActiveSheet.Cells(ActiveCell.Row + 1, 1).Select
End If
End Sub
Private Sub Workbook_Activate()
Application.OnKey "{TAB}", "Worksheet_Change" 'TAB key press
Application.OnKey "~", "Worksheet_Change" 'Keyboard ENTER press
End Sub
Private Sub Workbook_Deactivate()
Application.OnKey "{TAB}"
Application.OnKey "~"
End Sub
3条答案
按热度按时间tvmytwxo1#
Tab或Enter键已经触发
SelectionChange
事件。因此,如果您不需要使用
Change
事件而不是SelectionChange
事件,这可能是一种更整洁的方式。quhf5bfb2#
嗯...经过大量的实验,从很多地方收集代码片段,然后调试,我最终得到了下面的VBA宏。希望它能有所帮助!:)
1.按下
TAB
或ENTER
键时,Sub Worksheet_Change
将运行。1.它将检查是否保留了
F
列...1.如果为真=〉插入新行并选择第一个单元格
[A]n
,其中n
=行号。VBA宏代码
0ve6wy6x3#
也许我在这个问题上遗漏了一些东西,但是如果你选择你的六列,并用“创建列表”命令转换选择,那么每当你跳到一行的最后一个单元格时,你会自动跳到下一行。而且,如果你在最后一行,一个新的单元格会被创建。我不知道为什么你需要一个宏来做这个?