Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 102
Default VBA Example of Using WHILE and LOOP?

Hi everyone,

Can someone please refer me to an example on how to use WHILE and LOOP
in Excel's VBA?

Thanks,
Mike
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 230
Default VBA Example of Using WHILE and LOOP?

On 13/10/2011 15:31, Mike wrote:
Hi everyone,

Can someone please refer me to an example on how to use WHILE and LOOP
in Excel's VBA?

Thanks,
Mike


DO WHILE/UNTIL condition
something
LOOP

or

DO
something
LOOP WHILE/UNTIL condition

or

WHILE condition
statements
WEND

F1 on the keyword should get you syntax related help on a good day.

Regards,
Martin Brown
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 829
Default VBA Example of Using WHILE and LOOP?

"Mike" wrote:
Can someone please refer me to an example on how
to use WHILE and LOOP in Excel's VBA?


In VBA Help, type "while wend statement" and "do loop statement" without
quotes, and you will get lots of information. A few simple examples....

x = 1
While x <= y
[...some statements...]
x = x + 1
Wend

x = 1
Do While x <= y
[...some statements...]
x = x + 1
Loop

x = 1
Do Until x y
[...some statements...]
x = x + 1
Loop

x = 1
Do
[...some statements...]
x = x + 1
Loop While x <= y

x = 1
Do
[...some statements...]
Loop Until x y

The first 3 are equivalent to:

For x = 1 to y
[...some statements...]
Next


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 102
Default VBA Example of Using WHILE and LOOP?

On Oct 13, 11:15*am, "joeu2004" wrote:
"Mike" wrote:
Can someone please refer me to an example on how
to use WHILE and LOOP in Excel's VBA?


In VBA Help, type "while wend statement" and "do loop statement" without
quotes, and you will get lots of information. *A few simple examples.....

x = 1
While x <= y
* * [...some statements...]
* * x = x + 1
Wend

x = 1
Do While x <= y
* * [...some statements...]
* * x = x + 1
Loop

x = 1
Do Until x y
* * [...some statements...]
* * x = x + 1
Loop

x = 1
Do
* * [...some statements...]
* * x = x + 1
Loop While x <= y

x = 1
Do
* * [...some statements...]
Loop Until x y

The first 3 are equivalent to:

For x = 1 to y
* * [...some statements...]
Next


What if you have two or more conditions instaed of one?
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 102
Default VBA Example of Using WHILE and LOOP?

On Oct 13, 11:35*am, Mike wrote:
On Oct 13, 11:15*am, "joeu2004" wrote:





"Mike" wrote:
Can someone please refer me to an example on how
to use WHILE and LOOP in Excel's VBA?


In VBA Help, type "while wend statement" and "do loop statement" without
quotes, and you will get lots of information. *A few simple examples.....


x = 1
While x <= y
* * [...some statements...]
* * x = x + 1
Wend


x = 1
Do While x <= y
* * [...some statements...]
* * x = x + 1
Loop


x = 1
Do Until x y
* * [...some statements...]
* * x = x + 1
Loop


x = 1
Do
* * [...some statements...]
* * x = x + 1
Loop While x <= y


x = 1
Do
* * [...some statements...]
Loop Until x y


The first 3 are equivalent to:


For x = 1 to y
* * [...some statements...]
Next


What if you have two or more conditions instaed of one?- Hide quoted text -

- Show quoted text -


It is not exactly plain WHILE/Loop. Let me write you how I have the
While Loop thing:

WHILE( ( PlnHrznTop <= CARD(ts))
AND Continue < 0)),

!! t is the first time period in the upcoming solve
LOOP( t with condition that (ORD(t) = PlnHrznTop),



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 102
Default VBA Example of Using WHILE and LOOP?

On Oct 13, 11:35*am, Mike wrote:
On Oct 13, 11:15*am, "joeu2004" wrote:





"Mike" wrote:
Can someone please refer me to an example on how
to use WHILE and LOOP in Excel's VBA?


In VBA Help, type "while wend statement" and "do loop statement" without
quotes, and you will get lots of information. *A few simple examples.....


x = 1
While x <= y
* * [...some statements...]
* * x = x + 1
Wend


x = 1
Do While x <= y
* * [...some statements...]
* * x = x + 1
Loop


x = 1
Do Until x y
* * [...some statements...]
* * x = x + 1
Loop


x = 1
Do
* * [...some statements...]
* * x = x + 1
Loop While x <= y


x = 1
Do
* * [...some statements...]
Loop Until x y


The first 3 are equivalent to:


For x = 1 to y
* * [...some statements...]
Next


What if you have two or more conditions instaed of one?- Hide quoted text -

- Show quoted text -


My WHILE/LOOP structure is as follows:

WHILE condition1 and condition2
LOOP over t while condition3 as below:

WHILE( PlnHrznTop <= ts
AND Continue < 0),

LOOP( t while (ORD(t) = PlnHrznTop),
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 829
Default VBA Example of Using WHILE and LOOP?

"Mike" wrote:
It is not exactly plain WHILE/Loop. Let me write
you how I have the While Loop thing:

WHILE( ( PlnHrznTop <= CARD(ts))
AND Continue < 0)),
!! t is the first time period in the upcoming solve
LOOP( t with condition that (ORD(t) = PlnHrznTop),


"Mike" wrote:
My WHILE/LOOP structure is as follows:
WHILE condition1 and condition2

LOOP over t while condition3 as below:
WHILE( PlnHrznTop <= ts
AND Continue < 0),
LOOP( t while (ORD(t) = PlnHrznTop),


I do not understand your pseudocode. I don't know if ts and t are intended
to be the same (typo?) or different. Unless you can write your pseudocode
or algorithm clearly, it is hopeless to try to help you.

Here are some ideas that might be helpful. Then again, maybe not.

If PlnHrznTop <= ts And Continue < 0 Then
Do
[...some code...]
Loop While ORD(t) = PlnHrznTop
End If

--or--

Do While PlnHrznTop <= ts And Continue < 0
[...some code...]
If ORD(t) = PlnHzrnTop Then Exit Do
[...some more code...]
Loop

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 829
Default VBA Example of Using WHILE and LOOP?

PS....

"Mike" wrote:
LOOP over t while condition3 as below:
WHILE( PlnHrznTop <= ts
AND Continue < 0),
LOOP( t while (ORD(t) = PlnHrznTop),


Two more possible interpretations of your cryptic pseudocode come to mind.
Again, this might be misdirection.

If PlnHrznTop <= ts And Continue < 0 Then
Do
[...some code...]
Loop While ORD(t) = PlnHrznTop And PlnHrznTop <= ts And Continue < 0
End If

--or--

Do While PlnHrznTop <= ts And Continue < 0
Do
[...some code...]
Loop While ORD(t) = PlnHrznTop
Loop

PS: The outer Do While...Loop could be written using While...Wend. I kept
it as Do While to demonstrate that the looping constructs can be nested. I
did not want you to misunderstand that one must be While...Wend if the other
is Do...Loop.

PPS: I tend to use Do While...Loop instead of While...Wend because we can
use Exit Do in the middle. There is no Exit While; we must use Go To. No
biggie; it's just a style thing.

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
Naming Worksheets - Loop within a loop issue klysell Excel Programming 0 March 27th 07 11:17 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 06:28 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"