Nick Stauner
0
Q:

excel vba while... "end while" doesn't work?

'In VBA, there is no 'While... End While' loop.

'Attempting to use the above construct will result in a compile-time
'error.

'But VBA does have two loops that use the keyword, 'While'...

While c < 100
	c = c + 1
	'But VBA does not have an 'Exit While' statement.
Wend

Do While c < 100
	c = c + 1
	If c = 75 Then Exit Do
Loop


'The 'Do Loop' can also be written as:      
Do 
	c = c + 1
	If c = 75 Then Exit Do
Loop While c < 100
            
'--------------------------------------------------------------------
            
'The loops can also run backward...
            
c = 99
While c
    'Do something with c here
	c = c - 1
Wend           

c = 99
Do 
    'Do something with c here
	c = c - 1
Loop While c           
2

New to Communities?

Join the community