Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nested loops - not fully executing

Status
Not open for further replies.

edcharleslynn

Programmer
Jul 20, 2004
11
GB
Hello, i have a problem which seems like it should be basic but i cant work it out.
I have two arrays, and need to check which elements in the first array do not exist in the second. I simply loop through the one array, and have a second loop checking it against the elements in the second array. If it does not find a match, the flag is left set at 0, and so the element should display using MSGBOX. This works however only for the last none match it finds, not all. Can anyone please point out why this might be happening.

My code is as as follows:

Code:
Dim arrayElement1
Dim arrayElement2
If sourcesize <> thesesize Then
   For Each arrayElement1 In thesecodes
      flag = 0
      For Each arrayElement2 In sourcecodes
         If arrayElement1 = arrayElement2 Then
	    flag = 1
	    Exit For
	 End If
      Next

      If flag <> 1 Then
         msgbox arrayElement1
      End If			
   Next
End If

Thanks very much

Ed Lynn
 
Except for indentation your code seems OK, ie it should display all the elements of thesecodes not in sourcecodes.
Anyway, I personally use the method:
If sourcesize <> thesesize Then
sourcelist = ":" & Join(sourcecodes, ":") & ":"
For Each arrayElement1 In thesecodes
If InStr(sourcelist, ":" & arrayElement1 & ":") = 0 Then
MsgBox arrayElement1
End If
Next
End If

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks, that didnt work, but it turns out it was because i wasnt preserving the arrays when i was doing the Redim. This seems odd to me though, as it still finds the one none match. Perhaps this must have been the last one added, but i hadnt thought it was. Oh well, thanks for the help!
 
As usual, posting ALL the relevant code is better than than waiting for cristal ball advice.
 
Obviously if i had been aware that the way i created the arrays was relevant i would have posted the code associated with it. I only became aware when stumbling over a similar problem in another forum. Sorry to have wasted your time PHV, hope the fortune telling goes well.
 
Private dictionary:

Dictionary: "That didn't work"
It means the script segment did work the way it supposed to work, only that some butterfly flaps its wings somewhere around the globe making the whole thing collapses.

- tsuji
 
That is true. Perhaps i should have said that changing the code did not resolve the problem, not that it 'didnt work'. I'll be more careful about the way i say things in future ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top