Excel条码库存

Excel barcode inventory

我正在尝试制作一个 Excel 工作簿来跟踪库存余额。 现在我的工作簿设置了库存、存款和取款 sheets。库存 sheet 包含每个库存商品的代码和数量。

我想在存款或取款时在 A1 单元格中输入项目代码 sheet,然后程序应该获取该编号并查看它是否与库存 sheet 中的任何内容匹配,如果它会执行,然后它应该将 1 添加到该项目的数量,或者删除,取决于输入的存款或取款 sheet。如果找不到匹配项,则应在库存 sheet 中创建一个新项目。之后,它应该清除 A1 单元格。

我有一台 Datalogic Quickscan 条码扫描仪,我会为每件库存商品创建条码,并使用扫描仪将条码输入作品sheets。当我扫描条形码时,它只输出一个数字,就像在连接到 PC 的传统键盘上输入的那样。

我受困于 VBA 将更新库存 sheet 的代码。我有下面的代码,它在库存 sheet 中创建一个单元格,我可以在其中扫描条形码,然后将其添加到列表中,但是如果我需要另一个我可以扫描并从中减去的单元格,我该怎么办数量代替?

Private Sub Worksheet_Change(ByVal Target As Range)

    Const SCAN_CELL As String = "F7"
    Const RANGE_BC As String = "A1:A500"
    Dim val, f As Range, rngCodes As Range

    If Target.Cells.Count > 1 Then Exit Sub
    If Intersect(Target, Me.Range(SCAN_CELL)) Is Nothing Then Exit Sub

    val = Trim(Target.Value)
    If Len(val) = 0 Then Exit Sub

    Set rngCodes = Me.Range(RANGE_BC)

    Set f = rngCodes.Find(val, , xlValues, xlWhole)
    If Not f Is Nothing Then
        With f.Offset(0, 2)
            .Value = .Value + 1
        End With
    Else
        Set f = rngCodes.Cells(rngCodes.Cells.Count).End(xlUp).Offset(1, 0)
        f.Value = val
        f.Offset(0, 1).Value = "enter description"
        f.Offset(0, 2).Value = 1
    End If

    Application.EnableEvents = False
    Target.Value = ""
    Application.EnableEvents = True

    Target.Select

End Sub

这是用户表单的解决方案。

创建新工作表或将现有工作表重命名为 Inventory

创建用户表单 UserForm1,如下所示:

将代码放入UserForm1模块:

Option Explicit

Private pbModeDeposit As Boolean

Private Sub UserForm_Initialize()

    ' Setup header
    ThisWorkbook.Sheets("Inventory").Range("A1:C1").Value = Array("Item Code", "Description", "Quantity")
    ' Set Deposit mode
    pbModeDeposit = True
    ' Indicate current mode
    ShowMode

End Sub

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

    Dim sItemCode As String
    Dim n As Long
    Dim i As Long
    Dim bExists As Boolean

    ' Check if enter key pressed
    If KeyCode = KeyCodeConstants.vbKeyReturn Then
        ' Cancel key to keep textbox in focus
        KeyCode = 0
        ' Check entire input code
        sItemCode = Me.TextBox1.Value
        Me.TextBox1.Value = ""
        Select Case True
            Case Not IsNumeric(sItemCode)
                ' Skip non-numeric values
                Exit Sub
            Case sItemCode = "10001990"
                ' Service code to switch to Deposit mode
                pbModeDeposit = True
                ShowMode
            Case sItemCode = "10000991"
                ' Service code to switch to Withdrawal mode
                pbModeDeposit = False
                ShowMode
            Case Else
                With ThisWorkbook.Sheets("Inventory")
                    .Range("A1:C1").Value = Array("Item Code", "Description", "Quantity")
                    ' Get last filled row number
                    n = .Cells(Rows.Count, 1).End(xlUp).Row
                    ' Check if scanned code exists
                    For i = 2 To n
                        bExists = .Cells(i, 1).Value = sItemCode
                        If bExists Then Exit For
                    Next
                    If bExists Then
                        ' Change quantity of existing item
                        .Cells(i, 3).Value = .Cells(i, 3).Value + IIf(pbModeDeposit, 1, -1)
                    Else
                        ' Add new item
                        .Cells(n + 1, 1).NumberFormat = "@"
                        .Cells(n + 1, 1).Value = sItemCode
                        .Cells(n + 1, 3).Value = IIf(pbModeDeposit, 1, -1)
                    End If
                End With
        End Select
    End If

End Sub

Private Sub CommandButton1_Click()

    ' Change mode
    pbModeDeposit = Not pbModeDeposit
    ' Indicate current mode
    ShowMode
    ' Keep textbox in focus
    Me.TextBox1.SetFocus

End Sub

Private Sub ShowMode()

    Me.CommandButton1.Caption = IIf(pbModeDeposit, "Deposit", "Withdrawal")

End Sub

将代码放入ThisWorkbook模块:

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)

    With UserForm1
        .Show
        .CommandButton1.SetFocus
        .TextBox1.SetFocus
    End With

End Sub

此外,您可以将 UserForm1 属性 ShowModal 更改为 False

当您扫描代码时,它会输入到 TextBox1。如果代码是 10001990 则存款模式切换,如果 10000991 则取款模式,这在文本框旁边的按钮上指示。 1000199010000991只是举例,可以更改。任何其他数字输入都会产生库存清单的计算和更新。请注意,代码存储为文本以避免溢出或自动转换为大数字的工程符号 E。