Excel 选项卡在一定数量的列后换行

问题描述 投票:0回答:3

我想设置一个 Excel 电子表格,用于使用条形码扫描仪输入数据。 条形码扫描仪发送条形码,然后发送选项卡或回车键,具体取决于其编程方式。

基本上我想设置一个 Excel 工作表,我们可以为每个项目扫描 6 个条形码,扫描仪每次都跳到下一列,然后当它到达第六列时,下一个选项卡将使它移动到新行下一个产品。

我希望这是有道理的。它可以在 MS Word 中完成...例如,如果您创建一个包含 6 列的表格并按 Tab 键 7 次,它将移动到下一行。 我想在 Excel 中执行此操作。 谢谢你

excel vba row barcode-scanner
3个回答
0
投票

TAB 或 ENTER 键已触发

SelectionChange
事件。

因此,如果您没有其他原因需要使用

Change
事件而不是
SelectionChange
事件,这可能是做同样事情的一种更简洁的方法。

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

0
投票

好吧...经过大量实验,从很多地方收集代码片段然后进行调试,我最终得到了以下 VBA 宏。希望能帮助到你! :)

  1. 当按下
    TAB
    ENTER
    键时,
    Sub Worksheet_Change
    将运行。
  2. 它会检查是否留下了
    F
    列...
  3. 如果为 true => 插入新行并选择第一个单元格
    [A]n
    ,其中
    n
    = 行号。

VBA宏代码

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

0
投票

也许我在这个问题上遗漏了一些东西,但是如果您选择六列并使用“创建列表”命令转换选择,那么每当您按 Tab 键到一行的最后一个单元格时,您都会自动转到下一行。此外,如果您位于最后一行,则会创建一个新行。我不确定为什么你需要一个宏?

© www.soinside.com 2019 - 2024. All rights reserved.