2015-11-06 169 views
0

由於太久,我一直希望能夠提供一些見解。如何檢查一個範圍內的單元格是否存在於另一個範圍內

我試圖循環一個單元格範圍(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是否存在價值。

+0

沒有讀過你的代碼,但通過標題,你嘗試過'相交'嗎? – findwindow

回答

2

試試這個:

主要問題是布爾值。開始每個循環爲假,然後當它發現它相等改變它爲真退出for,如果最後它沒有找到匹配它仍然是假的然後測試,並做你需要的。

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 
+0

你怎麼這麼快?! – findwindow

+1

@findwindow MAGIC :) –

+0

@ScottCraner您的代碼無法使用,但我很欣賞快速響應! varSKU1,varSKU2,sku1和sku2必須標註爲變體,因爲這是它們的數據類型。如果未正確設置,代碼會發生錯誤。即使標註爲變體,考慮其他更改,也會收到相同的錯誤輸出。 – DevBW

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 
+0

我也修正了它。不是vba有趣嗎? –

+0

@ScottCraner你可以這麼說 – DevBW

相關問題