I am trying to scan barcode and have Excel move to the right cell

时间:2016-04-04 16:52:12

标签: excel vba barcode barcode-scanner enter

I need to make the cursor move to the right cell in Excel when I scan a bar code, but currently it goes down. I don't want to change it in the Excel Options because I need to have the cell cursor go down when I hit enter on other sheets.

1 个答案:

答案 0 :(得分:1)

我找到的一个解决方案是我最初开始使用的代码块。

从插件下的开发人员打开一个ActiveX文本框,并将其放在要扫描的工作表中。选择A1,然后单击文本框。确保开发人员关闭设计模式视图(查看工具栏功能区)。

围绕它的**线是你需要改变的。这是设置为长度,但你也可以玩其他的东西,如案例。尝试使用12345作为值创建条形码。

Private Sub TextBox1_Change()
**If Len(TextBox1.Value) = 5 Then**
    ActiveCell.Value = TextBox1.Value
    ActiveCell.Offset(1).Activate
    Application.EnableEvents = False
    TextBox1.Activate
    TextBox1.Value = ""
End If

End Sub

案例。尝试使用" Waste R - 1"制作条形码。作为价值。

Private Sub TextBox1_Change()

Dim ws As Worksheet, v, k, i, j
Set ws = Worksheets("Sheet1")

v = TextBox1.Value
k = 0
i = 0
j = 0

Select Case v

Case "Waste R - 1": i = 2
k = 1
j = "Waste R - 1"
'Start i at whatever column you want to start at
'k is the type of case and if statement you want to run (I have several, but for simplicity, I have only attached one)


End Select

    If k = 1 Then

    ws.Cells(1, 1) = ws.Cells(1, 1).Value + 1
    ' adds number into cell (A1) to reference which column to be in
    ' Starts in Column A then adds a value of one to reference column B

    i = ws.Cells(1, 1)
    'Sets i = to the A1 value

    Cells(1, i).Value = j
    ' You may be able to set this to textbox1.value
    ' Says to put the Textbox Value into whatever column and row A

    TextBox1.Activate
    TextBox1.Value = ""
End If
End Sub

确保在单元格A1中放置值2以启动。

如果你想最终,你可以把它变成一个循环。 我确信有更简单的方法,但我制作了一个excel模板,将一个模块参考粘贴到VBA表中。它很容易更新。如果我可以帮助解决任何其他条形码问题,请告诉我