首页 > 解决方案 > Excel VBA在多列上比较2列表框

问题描述

我正在尝试制作一个简单的 VBA 用户窗体来检查 2 个 ListBox 但有 2 个条件

  1. 检查产品编号
  2. 如果产品编号正常,则检查产品的数量是否正常

我的用户表单

这张图片显示了我找到的代码(我在这里搜索并且几乎没有改变它)只是删除重复项。当然,我知道代码只是检查每个 ListBox 的 1 列。

但是经过长时间的搜索和尝试,我找不到任何方法来检查 listbox1 column2 和 listbox2 column1 THEN 如果它的相等检查 listbox1 column 4 等于 listbox2 colum 2

这是一个简单的库存应用程序,我正在尝试为自己制作。listbox2 将由一些条形码扫描仪填充。

Dim obj As Object
Set obj = CreateObject("Scripting.Dictionary")    

'1st ListBox
For i = 1 To ListBox1.ListCount - 1
    If Not obj.Exists(CStr(ListBox1.List(i, 1))) Then
        obj.Add CStr(ListBox1.List(i, 1)), vbNullString
    End If
Next
'2nd ListBox
For i = 1 To ListBox2.ListCount - 1
    If Not obj.Exists(CStr(ListBox2.List(i, 0))) Then
'add unique of list 2
     obj.Add ListBox2.List(i, 0), vbNullString
   Else

'delet duplicte of list 1
      obj.Remove ListBox2.List(i, 0)
    End If
Next

'add unique list to 3rd ListBox
Dim Key As Variant
     ListBox3.List = obj.Keys()
    lblerr.Caption = ListBox2.ListCount / 2

标签: excelvbalistboxcompare

解决方案


推荐阅读