对于我们的电子商务业务,我们有一个大型电子表格,用于管理库存并同步到我们的网站。目前,这是一个完全手动的过程。
我想知道如何添加两个按钮来执行以下操作。
产品示例..
Column A | Column B
Product Name | Stock
---------------------+--------------
Product1_Variation1 | 50
Product1_Variation2 | 50
Product1_Variation3 | 50
Product1_Variation4 | 50
Product2_Variation1 | 50
Product2_Variation2 | 50
Product2_Variation3 | 50
Product2_Variation4 | 50
在另一列中,我将输入要更新的产品,即 IE 在 D1 列中键入 Product1。然后有两个按钮...
缺货 - 将搜索 A 列中文本包含 Product1(在 D1 中)并将库存更改为 0 的所有产品。
有货 - 将搜索 A 列中文本包含 Product1(在 D1 中)的所有产品,并将库存更改为 50。
我知道这需要一些 VB 代码,这就是我会挣扎的地方。
编辑
Sub button_1_oos_aw()
change_stock Range("B2").Text, Range("B3").Text, 0, Range("N2").Text, "OOS", Range("L2").Text
End Sub
Sub button_1_bis_aw()
change_stock Range("B2").Text, Range("B3").Text, 50, Range("N1").Text, "BIS", Range("L2").Text
End Sub
Sub button_2_oos_vanh()
change_stock Range("B2").Text, Range("B3").Text, 0, Range("N3").Text, "OOS", Range("L3").Text
End Sub
Sub button_2_bis_vanh()
change_stock Range("B2").Text, Range("B3").Text, 50, Range("N1").Text, "BIS", Range("L3").Text
End Sub
Sub button_3_oos_unc()
change_stock Range("B2").Text, Range("B3").Text, 0, Range("N4").Text, "OOS", Range("L4").Text
End Sub
Sub button_3_bis_unc()
change_stock Range("B2").Text, Range("B3").Text, 50, Range("N1").Text, "BIS", Range("L4").Text
End Sub
Sub change_stock(looking_for, looking_for2, change_to, change_to_message, inorout, stocktype)
If MsgBox("Are you sure you want to put " + looking_for + " width " + looking_for2 + inorout + " as type " + stocktype, vbYesNo) = vbNo Then Exit Sub
For y = 20 To Application.WorksheetFunction.CountA(Range("A:A"))
If InStr(Cells(y, 7), (looking_for)) > 0 And InStr(Cells(y, 12), (looking_for2)) > 0 Then
Cells(y, 15) = change_to
Cells(y, 17) = change_to_message
End If
Next
End Sub
约翰,我假设您知道如何添加按钮并在单击时分配要运行的宏?如果是这样,下面的代码应该可以完成你所追求的。
Sub button_1()
change_stock Range("D1").Text, 0 ' set stock value to zero
End Sub
Sub button_2()
change_stock Range("D1").Text, 50 ' set stock value to 50
End Sub
Sub change_stock(looking_for, change_to)
' Loop to look at each cell in column A
For y = 2 To Application.WorksheetFunction.CountA(Range("A:A"))
If Cells(y, 1) = looking_for Then ' <- if cell in column A matches looking_for value
'change the value
Cells(y, 2) = change_to
End If
Next
End Sub
这也使得假设数据中没有间隙,如果这是可能的,您将需要一种不同的方法来决定在哪里停止 Y 循环。