View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein Rick Rothstein is offline
external usenet poster
 
Posts: 5,934
Default EXCLUDING DUPES IN STRING ARRAY !!!!

You could maintain what I would call a "check string" for this purpose.
Let's say the name of this String variable is CheckString. Then you can do
this in a loop...

For X = 1 To SomethingLessThanInfinity
'
' Some kind of conditioning code goes here I presume
'
If YourCondition Then
If Instr(CheckString, Chr(1) & B & Chr(1)) = 0 Then
Arr(X) = B
CheckString = CheckString & Chr(1) & B & Chr(1)
End If
Next

If the text value in variable B is not in CheckString, then this is the
first time you have seen its value, so assign it to the array and then store
its value, with a delimiter on both sides of it, into CheckString. I have
used Chr(1) as my delimiter because under normal circumstances it will not
appear in any of the text being assigned to B during the loop. You can use
any character (or characters) that you **know** for certain will never
appear in your text strings for the delimiter. The reason you need this
delimiter is to stop accidental substring finds crossing over between your B
values. For example, if two consecutive values being assigned to B during
the loop were "moth" and "error" and did not use a delimiter between them,
then they would go into the CheckString as "...motherror..." and the latter
assignment of "mother" to B would register as already having been added to
the array... the delimiters guarantee this won't happen.

--
Rick (MVP - Excel)



"jay dean" wrote in message
...
Hi -

B is a string var. In my code, if a certain condition is met, then store
B in th next available index of Arr(). However, before I store B, I need
to check that the current value of B does not already exist in Arr().

Is there a "faster" way to accomplish this, or I need to loop from
lbound(Arr) to Ubound(Arr) every time to check if the new value to be
stored already exists?

Thanks
Jay Dean



*** Sent via Developersdex http://www.developersdex.com ***