Login | |
|
 |
Compare two arrays and store matching items into new array - 12/10/2006 11:47:09 AM
|
|
 |
|
| |
mrmore
Posts: 5
Score: 0
Joined: 11/9/2006
Status: offline
|
Ive used this function to process large amounts of data. Hopefully someone else will also find it useful. Compares array1 to array2 and the results are assigned to the function. Option Explicit Dim array1, array2, arrmatch, item array1 = Array("item1", "item2", "item3", "item4", "item5", "item6", "item7", "item8", "item9", "item10") array2 = Array("item4", "item5", "item6", "item7", "item8", "item9", "item3", "item11", "item12", "item13") arrMatch = CompArrays(array1, array2) If IsArray(arrMatch) then For each item in arrMatch MsgBox item Next Else MsgBox arrMatch End If Private Function CompArrays(ByVal array1, array2) Dim intMatching' as Integer Dim arrItem1, arrItem2, arrMatch, i Dim bolMatch' as Boolean Dim strMatch' as String intMatching = 0 If IsArray(array1) AND IsArray(array2) Then ' Count matching items to define array For each arrItem1 in array1 bolMatch = 0 For each arrItem2 in array2 If UCase(arrItem1) = UCase(arrItem2) Then bolMatch = 1 Exit For End If Next If bolMatch = 1 Then intMatching = intMatching + 1 End If Next If intMatching > 0 Then ReDim arrMatch(intMatching - 1) For each arrItem1 in array1 bolMatch = 0 For Each arrItem2 in array2 If UCase(arrItem1) = UCase(arrItem2) Then bolMatch = 1 Exit For End If Next If bolMatch = 1 Then For i = 0 to intMatching If VarType(arrMatch(i)) = 0 Then arrMatch(i) = arrItem1 Exit For End If Next End If Next Else strMatch = "Failed: Matching items not found" End If Else strMatch = "Failed: Input not an array" End If If InStr(1, strMatch, "Failed", 1) Then CompArrays = strMatch Else CompArrays = arrMatch End If End Function
< Message edited by mrmore -- 12/10/2006 2:10:06 PM >
|
|
| |
|
|
|
|
|