Excel barcode inventory

旧时模样 提交于 2021-02-19 09:42:42

问题


I'm trying to make an Excel workbook to track a stock balance. Right now my workbook is set up with an Inventory, Deposit and Withdrawal sheets. Inventory sheet contains codes and quantity for each item in stock.

I want to enter the item code in A1 cell on either the Deposit or Withdrawal sheet, then a program should take the number and see if it matches anything in the Inventory sheet, if it does then it should either add 1 to quantity of that item, or remove, depends on the Deposit or Withdrawal sheet, where input was made. If it can't find a match it should then create a new item in the Inventory sheet. After that, it should clear A1 cell.

I have a Datalogic Quickscan barcode scanner, I will create a barcode for each item in stock, and use the scanner to enter barcodes to the worksheets. When I scan the barcode it just outputs a number, as it would be typed on a conventional keyboard connected to PC.

I'm stuck with VBA code which will update Inventory sheet. I have the below code which makes a cell in the inventory sheet where I can scan the barcode and then it adds it in the list, but what would I do if I need another cell where I can scan and that subtracts from the quantity instead?

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

回答1:


Here is the solution with userform.

Create a new worksheet or rename existing to Inventory.

Create a userform UserForm1 as shown below:

Put the code to UserForm1 module:

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

Put the code to ThisWorkbook module:

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

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

End Sub

Also, you may change UserForm1 property ShowModal to False.

When you scan a code, it is input into TextBox1. If the code is 10001990 then Deposit mode switched, if 10000991 then Withdrawal mode, that is indicated on the button next to textbox. 10001990 and 10000991 just taken as the example and can be changed. Any other number input produces calculation and update of the inventory list. Note the codes are stored as text to avoid overflow or autoconversion to engineering notation E for big numbers.



来源:https://stackoverflow.com/questions/54531202/excel-barcode-inventory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!