Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22
Default For To / Next loop doesn't update...

How come this sub only runs through i=1 and i=2, when k is obviously
updated during loop?

Sub Test()

k = 2
For i = 1 To k
Debug.Print k
If k = 2 Then k = 4
Next i

End Sub

I know this sub solves the problem, but I find Do While loops to be
clumsy - any other solutions?

Sub test2()

k = 2
i = 1
Do While i <= k
Debug.Print k
If k = 2 Then k = 4
i = i + 1
Loop


End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22
Default For To / Next loop doesn't update...

Better examples - but same result :)

Sub Test()

k = 4
For i = 1 To k
Debug.Print "k = " & k & " i = " & i
If i = 2 Then k = 8
Next i

End Sub

Sub test2()

k = 2
i = 1
Do While i <= k
Debug.Print "k = " & k & " i = " & i
If k = 2 Then k = 4
i = i + 1
Loop


End Sub
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default For To / Next loop doesn't update...

Because the loop limit is set when it executes the For statement, it does
not get updated just because the variable that initially set it gets
changed.

--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)



"TFriis" wrote in message
...
How come this sub only runs through i=1 and i=2, when k is obviously
updated during loop?

Sub Test()

k = 2
For i = 1 To k
Debug.Print k
If k = 2 Then k = 4
Next i

End Sub

I know this sub solves the problem, but I find Do While loops to be
clumsy - any other solutions?

Sub test2()

k = 2
i = 1
Do While i <= k
Debug.Print k
If k = 2 Then k = 4
i = i + 1
Loop


End Sub



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
returning back to loop check condition without completing the loop ashish128 Excel Programming 13 April 3rd 08 12:53 PM
Loop to Filter, Name Sheets. If Blank, Exit Loop ryguy7272 Excel Programming 3 February 5th 08 03:41 PM
update range in For loop David Excel Programming 23 April 17th 06 10:39 PM
Advancing outer Loop Based on criteria of inner loop ExcelMonkey Excel Programming 1 August 15th 05 05:23 PM
Problem adding charts using Do-Loop Until loop Chris Bromley[_2_] Excel Programming 2 May 23rd 05 01:31 PM


All times are GMT +1. The time now is 08:59 AM.

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"