设置库存上下限

Setting High and Low Limit for Inventory

所以我 运行 在 VBA 上遇到了一些麻烦。我一直在从事一个库存项目,就在我即将完成它时,我被抛出了一项涉及 VBA 的额外任务。我对 VBA 非常缺乏经验,所以希望有人能指出我正确的方向。

Unfortunately, I cannot post actual pictures of the inventory sheets, so I will make a mock-up. The actual quantities of the items are in column G, starting at row 10 down to row 2084. 

Example: 2 8 4 13 29 29 38 55 8 75.

我的客户想要的是使用 VBA 脚本设置库存数量的上限和下限。请求的参数如下:

**Low Limit:** 
IF value is 10 or less - set low limit to 10;
IF value is between 10 and 30 - set low limit to 10;
IF value is 30 or greater - set low limit to 30;

**High Limit:** 
50% more than what each value is, rounded to the nearest whole number, unless that value is 10 or below. In which case the high limit would be 15, since the low limit of those values is set to 10. 

我目前的库存数量是初始数量。这些数量将随着库存 removed/added 的变化而改变。但是,这些初始数量将用作 high/low 限制的基准。所以我的想法是初始数量需要存储到一个数组中,数组中的那些初始数量将用于计算 high/low 限制,即使在更新库存时也是如此。如果那不可能,我将只在电子表格中添加一个初始库存列,但它是一个固定模板,所以他们希望我不这样做。

此外,在 运行 运行 VBA 脚本以确定 high/low 限制后,如果该值低于下限,我希望单个数量单元格变为红色,并且如果值高于上限,则为蓝色。显然,第一次是 运行 时,不会有高于上限的值,因为这些值将用于计算上限。但是,所有最初低于 10 的数量的单元格都会变为红色,因为它们开始时低于定义的下限。

我知道这很多,但由于我不确定从哪里开始,所以任何帮助将不胜感激。谢谢!!

我相信这就是您所需要的。

如果你想保存初始值的上限,你需要创建新的sheet调用它作为ex。配置并使用普通 excel 函数创建上限值。 (在 Sheet1 中,我创建了您的示例值列表)

=ROUND(IF(Sheet1!A1<=10; 15;Sheet1!A1*1,5);0)

然后您需要复制此单元格并仅粘贴 Config 中的值而不是公式。

这里有可以满足您要求的代码。

Private Sub prcCheckLimits()
    Dim ws As Worksheet: Set ws = ThisWorkbook.ActiveSheet
    Dim wsHighLimit As Worksheet: Set wsHighLimit = ThisWorkbook.Sheets("Config")
    Dim lngRow As Long 'lastRow
    Dim i As Long

    lngRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

    For i = 1 To lngRow
        With ws
            If .Cells(i, 1).value < fncLowLimit(.Cells(i, 1).value) Then
                .Cells(i, 1).Interior.ColorIndex = 3
            ElseIf .Cells(i, 1).value > wsHighLimit.Cells(i, 1).value Then
                .Cells(i, 1).Interior.ColorIndex = 37
            Else
                .Cells(i, 1).Interior.ColorIndex = 0
            End If
        End With
    Next i
End Sub

Private Function fncLowLimit(value As Long) As Long
    Dim result As Long

    If value < 30 Then
        result = 10
    Else
        result = 30
    End If

    fncLowLimit = result
End Function