Excel条码库存

时间:2019-02-05 09:29:11

标签: excel vba barcode

我正在尝试制作一个Excel工作簿来跟踪库存余额。 现在,我的工作簿中设置了“库存,存款和取款”表。库存表包含库存中每个项目的代码和数量。

我想在“存款”或“提款”表的A1单元格中输入物料代码,然后程序应获取编号,并查看其是否与“库存”表中的任何内容匹配,如果匹配,则应在数量上加1该项的选择或删除取决于输入的存款或取款单。如果找不到匹配项,则应在“库存”表中创建一个新项目。之后,应清除A1单元格。

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

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

sqrt

1 个答案:

答案 0 :(得分:1)

这是用户窗体的解决方案。

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

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

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。