Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 133
Default Looping : For... Next problem

I modified the following code to loop thru rows 2 to 5.
The strInitial wont reset to take the value of the next row, Itll remain
showing the value of the first row for as many times there are in N, all the
other variables reset to the next N.
Any suggestions?

For N = 2 To 5 'rows
For i = 0 To UBound(myArray)

If myArray(i) 0 Then

'Concatenate array values with column values to create a range for the
replacement string
strToReplace = "[" & myArray(i) & "]" 'Create string to be replaced from
array value
strRange = myArray(i) & N 'column and row
strReplacement = Range(strRange) 'Create the replacement string
'Replace existing string with replacment string
strInitial = Replace(strInitial, strToReplace, strReplacement, 1, 1) 'not
looping thru row
Else
i = UBound(myArray)
End If
Next i
MsgBox strInitial 'MsgBox displays the new string
Next N





  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default Looping : For... Next problem

It looks like your code starts off with strInitial having some initial
value; however you are changing the value inside the loop, so the next time
through the loop, it has the last value it was changed to. I would suggest
you create a new variable to do the modifications to, and reassign the
initial value on each loop. Here is what I am thinking.

For N = 2 To 5
CopyOfInitial = strInitial
For i = 0 To UBound(myArray)
If myArray(i) 0 Then
strToReplace = "[" & myArray(i) & "]"
strRange = myArray(i) & N
strReplacement = Range(strRange)
CopyOfInitial = Replace(CopyOfInitial, strToReplace, strReplacement,
1, 1)
Else
i = UBound(myArray)
End If
Next i
MsgBox CopyOfInitial
Next N

Here I created a variable named CopyOfInitial to modify within the inner
loop and which I reset at the beginning of the outer loop. That way, the
contents of strInitial are never destroyed and can be recalled at anytime.

Rick



"LuisE" wrote in message
...
I modified the following code to loop thru rows 2 to 5.
The strInitial wont reset to take the value of the next row, Itll remain
showing the value of the first row for as many times there are in N, all
the
other variables reset to the next N.
Any suggestions?

For N = 2 To 5 'rows
For i = 0 To UBound(myArray)

If myArray(i) 0 Then

'Concatenate array values with column values to create a range for the
replacement string
strToReplace = "[" & myArray(i) & "]" 'Create string to be replaced from
array value
strRange = myArray(i) & N 'column and row
strReplacement = Range(strRange) 'Create the replacement string
'Replace existing string with replacment string
strInitial = Replace(strInitial, strToReplace, strReplacement, 1, 1) 'not
looping thru row
Else
i = UBound(myArray)
End If
Next i
MsgBox strInitial 'MsgBox displays the new string
Next N






Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Looping problem rpw Excel Programming 0 May 23rd 07 10:57 PM
Looping Problem Paul Black Excel Programming 3 September 17th 05 09:59 AM
Looping problem Sleeping Bear Excel Programming 2 July 7th 05 07:41 PM
If Then Else looping problem Kieran1028[_12_] Excel Programming 1 November 11th 04 06:27 PM
Looping Problem Todd Huttenstine[_3_] Excel Programming 5 January 25th 04 12:51 AM


All times are GMT +1. The time now is 07:02 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"