读取条形码后跳转到Excel单元格以查看是否存在匹配项

时间:2018-08-01 21:33:29

标签: excel excel-vba barcode-scanner

我有一个带有条形码和产品详细信息的Excel工作表。我想使用条形码扫描仪在我的excel工作表中查找与这些条形码关联的项目(出于库存目的)并标记它们。我希望excel跳入相应的单元格。我已经尝试通过阅读类似的帖子来创建宏,而据我所知:

Private Sub CommandButton1_Click()

 Dim code As Variant
 Dim matchedCell As Range


    code = InputBox("Please scan a barcode and hit enter if you need to")
    Set matchedCell = Range("C2:C100").Find(what:=code, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=True)
    If Not matchedCell Is Nothing Then
    Range(matchedCell.Address).Interior.ColorIndex = 6
    Else: MsgBox "Barcode Not Found"
    End If

End Sub

这仅突出显示单元格。理想情况下,我想突出显示整个行。我也想跳到相应的单元格。我没有这方面的经验,只是在网上找到了两个不同的示例,直到我使它可以正常工作而没有错误消息。如何修改它以添加其他功能?

我也遇到了在输入框中输入条形码的问题。我所能做的就是通过扫描将其输入到单元格中,或者将数字手动输入到输入框中-如何使它在输入框中输入扫描的代码?光标在正确的位置,但是将被忽略。

谢谢

1 个答案:

答案 0 :(得分:0)

Private Sub CommandButton1_Click()

    Dim code As Variant Dim matchedCell As Range

    code = InputBox("Please scan a barcode and hit enter if you need to")

    Set matchedCell = Range("C2:C100").Find(what:=code, LookIn:=xlValues, _
                      lookat:=xlWhole, MatchCase:=True)

    If Not matchedCell Is Nothing Then
        With matchedCell
            Application.Goto .Cells(1)
            .Resize(1, 10).Interior.ColorIndex = 6
        End With
    Else
        MsgBox "Barcode Not Found"
    End If

End Sub