-3

我基本上想知道将工作表中的项目与列表框进行比较的最佳方法是什么,基本上,列表框中的项目需要在工作表上找到,如果找不到,则需要转到工作表的底部下一个空闲行,

这应该不会太难,但我尝试过使用 if 语句,但发现它必须搜索工作表上的所有行以及列表框中的所有行,这使得它有时滞后、耗时且无响应,

我正在考虑使用 .find 方法,但不想浪费我的时间,

代码更新:

`

          For i = 0 To Me.ListBox1.ListCount - 1

            field1 = Me.ListBox1.List(i)
            field2 = Me.ListBox1.List(i, 1)
            field3 = Me.ListBox1.List(i, 2)


            field2ammend = Right(field2, Len(field2) - 7)


    For Each rCell In rRng.Cells






        If rCell.Value = field1 Then

            comp = field3
            name = field2ammend

            Sheets("Hair").Range("E" & rCell.Row) = comp
            Sheets("Hair").Range("F" & rCell.Row) = name

            Range("A" & rCell.Row & ":H" & rCell.Row).Interior.ColorIndex = 24

            countgood = countgood + 1




        Else




            ListBox2.AddItem (field2)

            'bal = bal + 1
            'Sheets("Hair").Range("B" & lastrows) = field1
            'Sheets("Hair").Range("E" & lastrows) = comp
            'Sheets("Hair").Range("F" & lastrows) = name
            'Range("A" & lastrows & ":H" & lastrows).Interior.ColorIndex = 24
            'lastrows = lastrows + 1
            'countbad = countbad + 1
        End If

下一个 rCell 下一个 i

`

有什么建议么,

谢谢,

4

1 回答 1

0

我不知道原因,但是,这有帮助吗?

Sub Weiter_Click()
Dim i As Integer
Dim varTemp As Variant

    varTemp = ListBox1.List '<-- for listboxes with one column. you have to edit this to pass your code

    For i = LBound(varTemp) To UBound(varTemp)
        If Columns("A:A").Find(varTemp(i, 0)) Is Nothing Then '<--- there is the column you're looking in
            Cells(1, 1).End(xlDown).Offset(1, 0).Value = varTemp(i, 0)
        Else
            Columns("A:A").Find(varTemp(i, 0)).Interior.ColorIndex = 3 '<--- 3 is red, idk what color you want
        End If
    Next
End Sub
于 2017-06-23T11:25:32.377 回答