excel如何在固定单元格输入产品编码,则数据库的产品数量就会自动增加或减少?

比如今天1号,我在B21处输入编码222222,则B6单元格数量就是1,输入两次数量就变成2, 日期要是2号,同样操作,则D6增加数据,如果是在出库单元格输入条码,则数据就自动减去

要用vba完成
自动判断日期, 编码,然后 单元格自己相加,每次+1
Private Sub Worksheet_Change(ByVal Target As Range)
Dim tr, tc
tr = Target.Row
tc = Target.Column
If tr = 21 And tc = 2 Then
x = WorksheetFunction.Match([B21], Range("a1:a19"), 0)
y = Day(Now) & "号"
N = WorksheetFunction.Match(y, Range("a3:aZ3"), 0)
MsgBox "货号:" & Cells(x, "A") & "日期:" & y & "入库"
Cells(x, N) = Cells(x, N) + 1
End If
'----
If tr = 21 And tc = 5 Then
x1 = WorksheetFunction.Match([E21], Range("a1:a19"), 0)
y1 = Day(Now) & "号"
N1 = WorksheetFunction.Match(y1, Range("a3:aZ3"), 0)
MsgBox "货号:" & Cells(x1, "A") & "日期:" & y1 & "出库"
Cells(x1, N1 + 1) = Cells(x1, N1 + 1) - 1
End If
End Sub追问

再加一个功能,e21单元格输入一串编码确定之后,编码就消失了,方便下次输入,不用每次都要先删除旧的编码,再重新输入新编码

追答

Private Sub Worksheet_Change(ByVal Target As Range)
Dim tr, tc
tr = Target.Row
tc = Target.Column
On Error Resume Next '
Application.DisplayAlerts = False
If tr = 21 And tc = 2 Then
x = WorksheetFunction.Match([B21], Range("a1:a19"), 0)
y = Day(Now) & "号"
N = WorksheetFunction.Match(y, Range("a3:aZ3"), 0)
MsgBox "货号:" & Cells(x, "A") & "日期:" & y & "入库"
Cells(x, N) = Cells(x, N) + 1
[B21] = ""
End If

'----
If tr = 21 And tc = 5 Then
x1 = WorksheetFunction.Match([E21], Range("a1:a19"), 0)
y1 = Day(Now) & "号"
N1 = WorksheetFunction.Match(y1, Range("a3:aZ3"), 0)
MsgBox "货号:" & Cells(x1, "A") & "日期:" & y1 & "出库"
Cells(x1, N1 + 1) = Cells(x1, N1 + 1) - 1
[E21] = ""
End If
End Sub

追问

怎么才能输入编码之后,按空格键不要让他跳到下一格呢,依然固定在B21 或 E21单元格里

追答

[B21] = ""

修改为
[B21] = ""

[B21] .SELECT

追问

出错了,显示无效或不合格的引用

追答

Private Sub Worksheet_Change(ByVal Target As Range)
Dim tr, tc
tr = Target.Row
tc = Target.Column
On Error Resume Next
If tr = 21 And tc = 2 Then
x = WorksheetFunction.Match([B21], Range("a1:a19"), 0)
y = Day(Now) & "号"
N = WorksheetFunction.Match(y, Range("a3:aZ3"), 0)
MsgBox "货号:" & Cells(x, "A") & "日期:" & y & "入库"
Cells(x, N) = Cells(x, N) + 1
[B21] = ""
[B21].Select
End If
'----
If tr = 21 And tc = 5 Then
x1 = WorksheetFunction.Match([E21], Range("a1:a19"), 0)
y1 = Day(Now) & "号"
N1 = WorksheetFunction.Match(y1, Range("a3:aZ3"), 0)
MsgBox "货号:" & Cells(x1, "A") & "日期:" & y1 & "出库"
Cells(x1, N1 + 1) = Cells(x1, N1 + 1) - 1
[E21] = ""
[E21].Select
End If
End Sub

追问

好像不行,输完编码按回车键还是会跳到下一格

追答

[E21].Select

这个就是 定位到E21
你这个应该是扫条码的
这边测试没问题

追问

我在中间加一个销售,录入出库时,那出库的数据就跑到销售下面去了,需要哪里修改一下

追答

+1
修改为+2
因为日期是合并单元格

温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-10-17
一般不这么使用,输入的一般是一个列表,每天一行。追问

这样没法实现?

追答

这样能做到,但是一丁点错误都不能犯,完全没有订正的机会。

追问

那如何做呢

第2个回答  2020-10-17
大哥,你这个明明就是扫码枪好吧
相似回答