You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Excel宏实现扫码后更新库存表对应商品库存的技术求助

库存更新宏解决方案

宏代码实现

打开Excel按Alt + F11进入VBA编辑器,插入模块后粘贴以下代码:

Sub UpdateInventory()
    Dim barcode As String
    Dim newStock As Variant
    Dim wsInventory As Worksheet
    Dim wsAmendments As Worksheet
    Dim foundRow As Range
    
    ' 绑定对应工作表
    Set wsInventory = ThisWorkbook.Sheets("inventory")
    Set wsAmendments = ThisWorkbook.Sheets("amendments")
    
    ' 获取扫码条码和新库存值
    barcode = wsAmendments.Range("B5").Value
    newStock = wsAmendments.Range("B7").Value
    
    ' 输入校验
    If barcode = "" Then
        MsgBox "请先扫码输入条码!", vbExclamation
        Exit Sub
    End If
    If Not IsNumeric(newStock) Then
        MsgBox "库存值必须为数字!", vbExclamation
        Exit Sub
    End If
    
    ' 在inventory表A列精准匹配条码
    Set foundRow = wsInventory.Range("A:A").Find(What:=barcode, LookIn:=xlValues, LookAt:=xlWhole)
    
    ' 执行更新或提示错误
    If Not foundRow Is Nothing Then
        wsInventory.Cells(foundRow.Row, "D").Value = newStock
        MsgBox "库存已成功更新!", vbInformation
        ' 可选:清空扫码区和输入区
        wsAmendments.Range("B5,B7").ClearContents
    Else
        MsgBox "未找到对应条码的商品!", vbExclamation
    End If
End Sub

按钮绑定步骤

  1. 切换到Sheet2(amendments),点击菜单栏开发工具→插入→选择按钮(窗体控件)
  2. 在合适位置拖拽绘制按钮,松开后弹出宏选择窗口,选中UpdateInventory宏并确定
  3. 修改按钮文字为“更新库存”,调整大小和位置到操作方便的区域

注意事项

  • 确保Sheet名称与代码中的"inventory"、"amendments"完全一致(区分大小写)
  • 打开文件时选择“启用内容”以允许宏运行,或在信任中心设置中开启宏权限
  • B7单元格需输入数字格式内容,否则会触发错误提示

内容的提问来源于stack exchange,提问作者MEL166

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.11 11:42:03