使用InStr函数将数组中的值与另一个匹配

时间:2016-10-13 20:50:31

标签: vba excel-vba excel

上下文

我正在使用代码并尝试对IsInArray(custom)Instr的函数进行一些更改,但会收到编译错误。

我只想改变

"If IsInArray(Worksheets("Sheet1").Cells(i, 11).Value, vAllSheet2Values)"

 If InStr(Worksheets("Sheet1").Cells(i, 11).Value, vAllSheet2Values)

但它给出了编译错误。我不能简单地改变功能吗?如果不是我该怎么办?

代码

            Sub remDup()
            Dim LR As Long, LRSheet2 As Long, i As Long, a As Long
            Dim vAllSheet2Values() As Variant

            LRSheet2 = Worksheets("Sheet2").Cells(Rows.Count, 3).End(xlUp).Row
            LR = Worksheets("Sheet1").Cells(Rows.Count, 11).End(xlUp).Row
            a = 2

            For i = 1 To LRSheet2 
                ReDim Preserve vAllSheet2Values(i)
                vAllSheet2Values(i) = Worksheets("Sheet2").Cells(i, 3).Value
            Next i

            For i = LR To 1 Step -1
                If IsInArray(Worksheets("Sheet1").Cells(i, 11).Value, vAllSheet2Values) Then
                    Worksheets("Sheet1").Rows(i).Copy Worksheets("Sheet3").Rows(a)
                    Worksheets("Sheet1").Rows(i).Delete
                    a = a + 1
                End If
            Next i
            End Sub

            Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
                IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
            End Function

1 个答案:

答案 0 :(得分:0)

根据LRLRSheet2的大小,此代码可能需要一段时间才能运行。这是使用InStr()比较每列中单元格的一种方法。

我定义了您的工作表,定义了Rows.CountLR的{​​{1}}部分,并添加了第二个LRSheet2来遍历For Loop中的每个项目1}}数组。

vAllSheet2Values()