根据2列中的值删除列表框项目 - vba

时间:2016-03-08 00:28:18

标签: excel vba listbox listboxitem listboxitems

我对VBA比较陌生,正致力于创建一些表单以帮助进行库存管理。

初始化表单时,会有一个列表框,用于从库存表中提取产品信息。每行有11列,其中包含产品ID,供应商,价格,库存商品等信息。还有三个复选框 - 低于标准的项目,标准的项目和高于标准的项目。 3个复选框值设置为True以开始,因为在初始化表单时,所有清单都显示在列表框中。

我正在尝试编写代码,以便在取消选中其中一个复选框时从列表框中删除产品。例如,如果我取消选中“低于标准的商品”,我想要所有库存中的商品数量为<要删除的该项(另一列)的par值。这将使列表框仅显示那些等于或高于标准的项目。

我该怎么做呢?我不确定如何引用列表框列中的值。

提前谢谢!!如果不清楚,请提问。

Private Sub UserForm_Initialize()

Dim lr As Long
Dim Inv As Worksheet
Dim rng As Range

Set Inv = Sheets("Inventory")
lr = Inv.Cells(Rows.Count, "A").End(xlUp).Row
Set rng = Inv.Range("A2:K" & lr)

Me.lbInventory.ColumnCount = 11
Me.lbInventory.RowSource = rng.Address
Me.lbInventory.ColumnHeads = True

Me.chkAbove.Value = True
Me.chkBelow.Value = True
Me.chkAt.Value = True

End Sub


Private Sub chkAbove_Change()
    ListBuild
End Sub

Private Sub chkAt_Change()
    ListBuild
End Sub

Private Sub chkBelow_Change()
    ListBuild
End Sub


Sub ListBuild()

Dim Inv As Worksheet
Set Inv = Sheets("Inventory")

Dim r As Integer
Me.lbInventory.Clear

If Me.chkBelow.Value = True Then
    For r = 1 To 11
        If Inv.Cells(r, 7).Value <
           Inv.Cells(r, 9).Value Then
                Me.lbInventory.AddItem Inv.Cells(r, 1).Value
        End If
    Next r
End If

If Me.chkAt.Value = True Then
    For r = 1 To 11
        If Inv.Cells(r, 7).Value =
           Inv.Cells(r, 9).Value Then
                Me.lbInventory.AddItem Inv.Cells(r, 1).Value
        End If
    Next r
End If

If Me.chkAbove.Value = True Then
    For r = 1 To 11
        If Inv.Cells(r, 7).Value >
           Inv.Cells(r, 9).Value Then
                Me.lbInventory.AddItem Inv.Cells(r, 1).Value
        End If
    Next r
End If

End Sub

我收到编译错误。这三个陈述的预期表达:

If Inv.Cells(r, 7).Value >,<,=
        Inv.Cells(r, 9).Value Then

1 个答案:

答案 0 :(得分:0)

可能有一种更清洁的方式,我鼓励任何有更清洁方式的人加入,但这似乎有效。基本上,在每个复选框的change事件下重建列表框列表。下面我举一个例子,假设复选框名为BelowPar,AtPar,&amp; AbovePar。

Private Sub AbovePar_Change()
    ListBuild
End Sub

Private Sub AtPar_Change()
    ListBuild
End Sub

Private Sub BelowPar_Change()
    ListBuild
End Sub

Sub ListBuild()

    Dim r As Integer
    InventoryList.Clear

    If BelowPar.Value = True Then
        For r = 1 To 11
            If Sheets("InventorySheet").Cells(r, 2).Value < _
               Sheets("InventorySheet").Cells(r, 3).Value Then
                    InventoryList.AddItem Sheets("InventorySheet").Cells(r, 1).Value
            End If
        Next r
    End If

    If AtPar.Value = True Then
        For r = 1 To 11
            If Sheets("InventorySheet").Cells(r, 2).Value = _
               Sheets("InventorySheet").Cells(r, 3).Value Then
                    InventoryList.AddItem Sheets("InventorySheet").Cells(r, 1).Value
            End If
        Next r
    End If

    If AbovePar.Value = True Then
        For r = 1 To 11
            If Sheets("InventorySheet").Cells(r, 2).Value > _
               Sheets("InventorySheet").Cells(r, 3).Value Then
                    InventoryList.AddItem Sheets("InventorySheet").Cells(r, 1).Value
            End If
        Next r
    End If

End Sub

根据您的需要调整范围和工作表名称。对于此示例,Cells(r, 1).Value =项目名称,Cells(r, 2).Value =库存商品数量,Cells(r, 3).Value =参数值。其中1,2,3是包含数据的列号。

相关问题