使用Excel VBA进行比较和执行功能
我正在用excel VBA为我的小餐厅制作一个项目,我设计了一个收据和转账表单,用于销售给客户或将物品转移到另一个部门(部门:商店、厨房和销售点)。我有一个工作表使用Excel VBA进行比较和执行功能,excel,vba,Excel,Vba,我正在用excel VBA为我的小餐厅制作一个项目,我设计了一个收据和转账表单,用于销售给客户或将物品转移到另一个部门(部门:商店、厨房和销售点)。我有一个工作表“库存”,其中包含所有部门的所有项目及其价格和可用数量 但是,当单击“关闭并保存”或“打印”按钮时,即当物品出售或转移时,我无法自动更新可用数量 这是我尝试过的代码 Sub updateinventory() Dim invlastrow, reclastrow As Long Dim ws1, ws2 As Worksheet Dim
“库存”
,其中包含所有部门的所有项目及其价格和可用数量
但是,当单击“关闭并保存”或“打印”按钮时,即当物品出售或转移时,我无法自动更新可用数量
这是我尝试过的代码
Sub updateinventory()
Dim invlastrow, reclastrow As Long
Dim ws1, ws2 As Worksheet
Dim itemName, itemQty, a, b
Set ws1 = ThisWorkbook.Worksheets("Inventory")
Set ws2 = ThisWorkbook.Worksheets("Receipt")
invlastrow = ws1.Cells(Rows.Count, 1).End(xlUp).Row
reclastrow = ws2.Cells(Rows.Count, 1).End(xlUp).Row
'MsgBox Cells(reclastrow, 3).Value
Do While Cells(invlastrow, 3).Value <> ""
itemName = Cells(invlastrow, 3).Value
For b = 19 To reclastrow
If itemName = Cells(reclastrow, 1) Then
MsgBox (itemName)
itemQty = Cells(invlastrow, 8).Value
itemQty = itemQty - Cells(reclastrow, 3)
End If
Next b
'MsgBox (itemQty)
End If
Next a
End Sub
Sub-updateinventory()
昏暗的invlastrow,与长的reclastrow相同
将ws1、ws2标注为工作表
尺寸项目名称、项目数量、a、b
Set ws1=ThisWorkbook.Worksheets(“库存”)
Set ws2=ThisWorkbook.Worksheets(“收据”)
invlastrow=ws1.Cells(Rows.Count,1).End(xlUp).Row
reclastrow=ws2.Cells(Rows.Count,1).End(xlUp).Row
'MsgBox单元格(reclastrow,3).值
Do While单元格(invlastrow,3)。值“”
itemName=单元格(invlastrow,3).值
对于b=19至reclastrow
如果itemName=单元格(reclastrow,1),则
MsgBox(项目名称)
itemQty=单元格(invlastrow,8).值
itemQty=itemQty-单元格(reclastrow,3)
如果结束
下一个b
'MsgBox(项目数量)
如果结束
下一个
端接头
如果您能够发布此代码所指的库存表部分的屏幕截图,那么它将非常有用。另外,您是否遇到了一个错误?或者仅仅是一些您期望之外的行为?“Dim itemName,itemQty,a,b”没有变量集。此外,您应该始终在代码顶部添加“Option Explicit”(在“Sub”之前)。好的,谢谢。我将发布一个指向文件和代码的链接