2010-09-20 40 views
0

我有一個問題,我只是無法修復。 我需要找到一個產品是否存在,因此它不應該被處理兩次(或更多)。這裏是我的代碼:VBA:循環找到多餘的值,不要把它處理

Dim table As Variant 
finalLig = Cells(Application.Rows.Count, 1).End(xlUp).row 
table= Range("C3:D" & finalLig).Value 'two dimensionnal array (nb of products, 2 columns. 2nd column is just here to create a multidimmensionnal array) 


For i = LBound(tableau) To UBound(table) 'For all values in the array (barcodes) 
    If table(i, 2) <> 1 Then 
    Valeur = table(i, 1)     'Value to check 
    For J = LBound(tableau) To UBound(tableau) 'Loop in the table 
     valeurComp = tableau(J, 1)   
     If Valeur = valeurComp And i <> J Then 'If two lines share the same code, then 
      table(i, 2) = 1 'remember that this value shouldn't be treated once in the i loop 
      'my actions here 
     End If 
    Next J 
    End If 
Next i 

在這裏,當我調試,我看到了(1,2)值永遠不會設置爲1,所以我的產品進行處理,兩次。我只是不明白爲什麼。

感謝您的幫助。

+0

你在表和表之間交換了幾次,但似乎指的是相同的Range對象 – barrowc 2010-09-20 17:26:23

回答

1

如果我正確理解你的代碼...

table(i, 2) = 1來自行If table(i, 2) <> 1 Then後,所以也沒有,如果你設置它,在一次回來到If聲明這將有重要循環,這樣我就會增加,它正在查看下一個值(尚未設置爲1)。

+0

對不起,我的代碼是不正確的。它是表(j,2)= 1。不知道爲什麼,但如果不是(表(i,2))= 1然後...解決了問題。無論如何,感謝您花時間看看我的代碼。 – Coronier 2010-09-20 10:18:34