由於太久,我一直希望能夠提供一些見解。如何檢查一個範圍內的單元格是否存在於另一個範圍內
我試圖循環一個單元格範圍(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爲新的細胞來表(「貓」),無論在varSKU2是否存在價值。
沒有讀過你的代碼,但通過標題,你嘗試過'相交'嗎? – findwindow