如何检查一个范围内的单元格是否存在于另一个范围内

时间:2015-11-06 19:12:50

标签: excel vba excel-vba nested

长期坚持这一点,我希望你们中的一个可以提供一些见解。

我试图遍历一个单元格范围(varSKU1),检查每个单元格范围内是否存在每个值(varSKU2)。如果该值不存在,我想将其添加为varSKU2中的新单元格。

以下代码:

Dim varSKU1(), varSKU2() As Variant
Dim n, m As Integer
Dim sku1, sku2 As Variant
Dim rowCount1, rowCount2 As Integer
Dim mFlag As Boolean

rowCount1 = Sheets("SKUs").Cells(Rows.Count, "A").End(xlUp).row
rowCount2 = Sheets("Cats").Cells(Rows.Count, "A").End(xlUp).row

varSKU1 = Sheets("SKUs").Range("A2:A" & rowCount1).Value
varSKU2 = Sheets("Cats").Range("A2:A" & rowCount2).Value

m = rowCount2 + 1

For Each sku1 In varSKU1

    mFlag = False

    For Each sku2 In varSKU2

        If sku1 = sku2 Then

            mFlag = False
            Exit For

        Else

            mFlag = True

        End If

    Next sku2

    If mFlag = True Then

        Sheets("Cats").Range("A" & m).Value = sku1
        Sheets("Cats").Range("B" & m).Value = "Misc"
        Sheets("Cats").Range("C" & m).Value = "Miscellaneous"

        m = m + 1

    End If

Next sku1

现在,代码将varSKU1中的所有值作为新单元添加到Sheets(" Cats"),无论varSKU2中是否存在该值。

2 个答案:

答案 0 :(得分:3)

试试这个:

主要问题是布尔值。将每个循环作为false启动然后当它发现它等于将其更改为true时退出for,如果最终它找不到匹配则它仍为false然后测试并执行您需要的操作。

Sub code()
Dim varSKU1 As Range, varSKU2 As Range
Dim n&, m&
Dim sku1 As Range, sku2 As Range
Dim rowCount1&, rowCount2&
Dim mFlag As Boolean

rowCount1 = Sheets("SKUs").Cells(Sheets("SKUs").Rows.Count, "A").End(xlUp).Row
rowCount2 = Sheets("Cats").Cells(Sheets("Cats").Rows.Count, "A").End(xlUp).Row

Set varSKU1 = Sheets("SKUs").Range("A2:A" & rowCount1)
Set varSKU2 = Sheets("Cats").Range("A2:A" & rowCount2)
m = rowCount2 + 1

For Each sku1 In varSKU1
    mFlag = False
    For Each sku2 In varSKU2
        If cstr(sku1) = cstr(sku2) Then
            mFlag = True
            Exit For
        End If
    Next sku2

    If mFlag = False Then
        Sheets("Cats").Range("A" & m).Value = sku1
        Sheets("Cats").Range("B" & m).Value = "Misc"
        Sheets("Cats").Range("C" & m).Value = "Miscellaneous"
        m = m + 1
    End If

Next sku1
End Sub

答案 1 :(得分:0)

感谢@ScottCraner,你当然帮助缩小了我的问题范围。我保留了你的布尔变化,但这里真正的决定因素是sku1和sku2比较。无论出于何种原因,比较两个范围/变体都很有吸引力。将它们转换为字符串后,我能够正确地引用它们的文本值。我之前假设,我们在内存中引用了它们的索引或地址。

Sub FindMisc()

Dim varSKU1 As Variant
Dim varSKU2 As Variant
Dim n, m
Dim sku1, sku2 As Variant
Dim rowCount1, rowCount2
Dim mFlag As Boolean


rowCount1 = Sheets("SKUs").Cells(Sheets("SKUs").Rows.Count, "A").End(xlUp).Row
rowCount2 = Sheets("Cats").Cells(Sheets("Cats").Rows.Count, "A").End(xlUp).Row

varSKU1 = Sheets("SKUs").Range("A2:A" & rowCount1)
varSKU2 = Sheets("Cats").Range("A2:A" & rowCount2)

m = rowCount2 + 1

    For Each sku1 In varSKU1

        mFlag = False

        For Each sku2 In varSKU2

            If CStr(sku1) = CStr(sku2) Then

                mFlag = True
                Exit For

            End If

        Next sku2

        If mFlag = False Then

            Sheets("Cats").Range("A" & m).Value = sku1
            Sheets("Cats").Range("B" & m).Value = "Misc"
            Sheets("Cats").Range("C" & m).Value = "Miscellaneous"

            m = m + 1

        End If

    Next sku1
End Sub