2016-08-03 58 views
-1

我在三張紙的A列中有數據。我必須記錄一張紙的數據,並且必須與其餘兩張紙的數據進行比較。發現重複的三張數據

任何人都可以幫助我嗎?

+0

你的問題很模糊。請給我們更多的細節。你想比較哪種數據?如果可能,請給我們一個簡短但實際的例子。一個很好的比較/匹配函數是vba中的'range.find'方法。看看[這裏](https://msdn.microsoft.com/en-us/library/office/ff839746.aspx)。 –

回答

0

沒有更多的上下文/信息,我只能給你一個高層次的迴應,但希望它能讓你開始。

要檢查是否在一個範圍內的項目在其他範圍內被發現,下面的代碼構建從一個範圍的字符串,然後使用「INSTR」對每個項目從反對另一個範圍進行比較。爲了做更復雜的比較,我會從範圍中構建數組。

Sub CompareLists() 
Dim rng1 As Range, rng2 As Range 
Dim cell As Range 
Dim tmp As String 

Set rng1 = Worksheets("Sheet1").Range("A1:A6") 
Set rng2 = Worksheets("Sheet2").Range("A1:A6") 

'Build pipe-delimited string from cells in range 
For Each cell In rng1 
    tmp = tmp & cell & "|" 
Next cell 

'Remove last pipe 
tmp = Left(tmp, Len(tmp) - 1) 

'Loop list 2 and compare against list 1. 
'Specifically, see if each item in list 2 is found in list 1 
For Each cell In rng2 
    If InStr(1, tmp, cell) > 0 Then 

     'Print items from list 2 that are found in list 1 
     Debug.Print "Found: " & cell.Value 

    Else 

     'Print items from list 2 that are NOT found in list 1 
     Debug.Print "NOT Found: " & cell.Value 

    End If 

Next cell 

Set rng1 = Nothing 
Set rng2 = Nothing 
Set cell = Nothing 

末次