简体   繁体   中英

How to check if a cell in one range exists in another range

Been stuck on this for far too long, I'm hoping one of you may provide some insight.

I'm attempting to loop through one range of cells (varSKU1), checking to see if each value exists in another range of cells (varSKU2). If the value doesn't exist, I want to add it as new cell in varSKU2.

Code below:

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

Right now, the code adds all values in varSKU1 as new cells to Sheets("Cats"), regardless of whether the value exists in varSKU2.

Try this:

The main issue was the boolean. Start each loop as false then when it finds it equal change it to true exit the for, if at the end it does not find a match it is still false then test on that and do what you need.

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

Thanks @ScottCraner, you certainly helped narrow down my issue. I kept your boolean change, but the real determining factor here was the sku1 and sku2 comparison. For whatever reason, comparing two ranges/variants was being finnicky. After converting them both to strings, I was able to correctly reference their textual value. I assume before, we were referencing either their index or address in memory.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM