Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Compare and update arrays
I have a problem with comparing two arrays and the looping logic is
doing my head in! Can anyone help please? I want to compare two arrrays and update the values in array 2 with the new values from array 1. The order of array 2 should be maintained. Therefore, any new elements in array 1 (not currently present in array 2) should be added to the end of array 2 For example: Array1 = ("a,1", "b,1", "c,1") Array2 = ("b,2", "a,2", "x,2") Updated Array 2 after comparison with Array 1 should be ("b,1", "a,1", "x,2", "c,1"). i.e. Where found, the first element remains 'as is'. If not already found in Array2, then it is added. The value of the second element is updated. Here's the code I've been attempting to create...but it's not working as it should... Sub Wash() iCount = 1 iRow = 1 Do While iRow <= UBound(Array1, 1) 'if elements in both arrays are equal then move on... Do While Array1(iRow, 1) < Array2(iCount, 1) 'but if not, then check if array2 is empty If IsEmpty(Array2(iCount, 1)) = True Then 'if empty, then add the new element to array2 Array2(iCount, 1) = Array1(iRow, 1) Array2(iCount, 2) = Array1(iRow, 2) Exit Do End If 'Otherwise, if the array is not empty, then increment the ' counter in array2 iCount = iCount + 1 ' Loop back, checking each of the elements in array2 for a match ' with array1. If no match is found then the value ' is added to array2 as above Loop ' Now if the compared elements match, then assign ' the other values to array2 Array2(iCount, 1) = Trim(Array1(iRow, 1)) Array2(iCount, 2) = Trim(Array1(iRow, 2)) 'Now that the values have been assigned, move on to ' the next element in array1 iRow = iRow + 1 Loop End Sub Please help! |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
How do I compare values in two arrays in Excel? | Excel Discussion (Misc queries) | |||
Compare arrays | Excel Worksheet Functions | |||
Compare arrays | Excel Worksheet Functions | |||
canceling the formula aut-update feature with Arrays | Excel Discussion (Misc queries) | |||
Arrays: compare fields? | Excel Programming |