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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stuck in a loop w/ an array problem

Status
Not open for further replies.

LuckyDuck528

Programmer
Dec 21, 2004
65
US
This should be simple but I'm just not seeing the error in my logic to make this work.

Below is my code and what I am trying to do is say:
1. Is this array exists, search for this string in the array
2. If you find a match. Flag it and move on in the program.
3. If you don't find a match. Add this string to the array
and print it in the next available spot.
4. If the array doesn't exists yet, create it and print
this at the first value.

The problem is that I can tweak and rearrange this thing as much as I want and sometimes I get an infinite loop, othertimes it won't print anything and other times it will only print the first value... depending on how it's arranged when running...

If anyone has any ideas I would be SO incredibly greatful for your help!

Thank You!

Code:
Flag = 0

			
If IsArray(arrComp) Then
   For intq = LBound(arrComp) To UBound(arrComp)
      If strm = arrComp(intq) Then
	 'Do Nothing
         Flag = 1
      End If
    Next
      If Flag <> 1 Then					
         ReDim Preserve arrComp(UBound(arrComp) + 1)
         arrComp(UBound(arrComp)) = strm	
         inthere=inthere+1		
         intHereTrack= intHereTrack + 1
	 ExcelBook.Worksheets(3).Cells(inthere,1).Value=strm
       Else 
	 arrComp = Array(strm)
	 inthere=inthere+1
	 intHereTrack= intHereTrack + 1
	 ExcelBook.Worksheets(3).Cells(inthere,1).Value=str
       End If 
End If
 
First, INDENT your code.
You may consider a dictionary object for easier manipulation.
Anyway, your code:
Flag = 0
If IsArray(arrComp) Then
For intq = LBound(arrComp) To UBound(arrComp)
If strm = arrComp(intq) Then
'Do Nothing
Flag = 1
End If
Next
If Flag <> 1 Then
ReDim Preserve arrComp(UBound(arrComp) + 1)
arrComp(UBound(arrComp)) = strm
inthere=inthere+1
intHereTrack= intHereTrack + 1
ExcelBook.Worksheets(3).Cells(inthere,1).Value=strm
End If
Else
arrComp = Array(strm)
inthere=inthere+1
intHereTrack= intHereTrack + 1
ExcelBook.Worksheets(3).Cells(inthere,1).Value=strm
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top