如何在Excel中使用宏管理库存

时间:2019-05-30 11:07:09

标签: excel vba macros

我正在尝试让Excel用作库存扫描读取器。 当我使用条形码扫描仪扫描任何物品时,它应该在Excel中识别该物品并添加我当前的库存。

在我的excel中,我有以下数据:COL A为Description,COL B为条形码,COL C为QTY_before出售,COL D为Current_scan_stock。

D列将为空,当我扫描条形码时,它应该识别该项目并添加数量,每次我扫描相同的条形码时,它应该添加+1

1 个答案:

答案 0 :(得分:0)

如果您要通过excel传递条形码,则可以使用以下代码:

Option Explicit

Sub test()

    Dim LastRow As Long
    Dim rngToSearch As Range, rngFound As Range
    Dim LookingValue As String

    'Create a with statement refer to the sheet that your data are store
    With ThisWorkbook.Worksheets("Sheet1")
        'Assigg to LookingValue the barcode scanned
        LookingValue = "1234"
        'Find the last row of column A
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        'Set the range to search for the barcode in
        Set rngToSearch = .Range("B2:B" & LastRow)
        'Set to rngFound the results from the find
        Set rngFound = rngToSearch.Find(LookingValue, LookIn:=xlValues)
        'If the result is nothing
        If rngFound Is Nothing Then
            'Message box
            MsgBox "Barcode was not found."
        'if you find a result
        Else
            'Add 1 to the existing value
            .Cells(rngFound.Row, 4).Value = 1 + .Cells(rngFound.Row, 4).Value
        End If

    End With

End Sub