2016-03-08 101 views
0

我对VBA相对较新,正在创建一些表单以帮助进行库存管理。基于2列中的值删除列表框项目 - vba

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

我正试图编写代码,当其中一个复选框未选中时,将从列表框中删除产品。例如,如果我取消选中“项目低于标准”,我希望所有产品库存中的项目数为<,该项目(另一列)的标准值将被删除。这将使列表框仅显示那些处于或高于标准的项目。

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

预先感谢您!如果不清楚,请提问。


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 

我得到一个编译错误。这3个语句表达的预期:

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

对于每个if语句,在操作符后面添加一个下划线以使其指向下一行。例如'如果Inv.Cells(r,7).Value> _'我最初在代码中忘记了它,但更新了我的答案 - 请参见下文。 – SincereApathy

+0

...或者将下一行('Inv.Cells(r,9).Value Then')移动到操作员的同一行上。我只是把它放在了我的例子中。 – SincereApathy

+0

@SincereApathy哦,当然!我很感谢 – sqlnewbie33

回答

0

有可能是一个更清洁的方式,我鼓励任何人谁拥有更清洁的方式来捧场,不过这似乎工作。基本上,重建每个复选框的更改事件下的列表框列表。下面我举一个例子,假设复选框名为BelowPar,AtPar,& 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是包含数据的列号。

+0

感谢您的回复!这种处理方式非常有意义!但是我得到错误的三个if语句的值是<, >,或= to。我上面发布了我的代码。有什么想法吗? – sqlnewbie33