Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default for next loop problems

Hi I am a excel programming beginner, so my question may be stupid

1. how to jump out from a for next loop without error?

eg

For LR = 5 To 25 Step 4
For LC = 1 To 5 Step 1
If ActiveSheet.cells(LR,LC).Value < True Then
Worksheets("Label-s").cells(LR, LC).Value = "abcde"
Worksheets("Label-s").cells(LR+1,LC).Value = "Art no. " & art & " " & des
Worksheets("Label-s").cells(LR+2,LC).Value = con & " " & hen & " " & w
GoTo 178
End If
Next LC
Next LR

I use (goto 178) to go to line 178 is this the right way?
(line 178 is the end of this macro)
-------------------------------------------------


2. I want the follow order for the for next loop

5,9,13,17,21,25
so I use this
For LR = 5 To 25 Step 4

I want 1,3,5
so I use
For LC = 1 To 5 Step 1

the result is that the variable fill in cells I do not wanted such as A8
A13, and b6 .......

the result what I want ( the cells which is fill in something) are like this....

a5 c5 e5
a6 c6 e6
a7 c7 e7

a9 c9 e9
a10 c10 e10
a11 c11 e11

............



thanks for help
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 400
Default for next loop problems

The recommended way to jump out of a for/next loop is to use the Exit For
statement i.e instead of GoTo 178 use Exit For: this will cause execution to
resume on the Next LR line.

Don't use absolute branching (GoTo 178): this style of coding is a nightmare
to maintain. It is arguable that you should not use GoTo at all. If you must,
use GoTo Label where Label is a valid name followed by colon. For example:

If a=0 then goto MyLabel
'.... other statements

MyLabel:


"cliee" wrote:

Hi I am a excel programming beginner, so my question may be stupid

1. how to jump out from a for next loop without error?

eg

For LR = 5 To 25 Step 4
For LC = 1 To 5 Step 1
If ActiveSheet.cells(LR,LC).Value < True Then
Worksheets("Label-s").cells(LR, LC).Value = "abcde"
Worksheets("Label-s").cells(LR+1,LC).Value = "Art no. " & art & " " & des
Worksheets("Label-s").cells(LR+2,LC).Value = con & " " & hen & " " & w
GoTo 178
End If
Next LC
Next LR

I use (goto 178) to go to line 178 is this the right way?
(line 178 is the end of this macro)
-------------------------------------------------


2. I want the follow order for the for next loop

5,9,13,17,21,25
so I use this
For LR = 5 To 25 Step 4

I want 1,3,5
so I use
For LC = 1 To 5 Step 1

the result is that the variable fill in cells I do not wanted such as A8
A13, and b6 .......

the result what I want ( the cells which is fill in something) are like this....

a5 c5 e5
a6 c6 e6
a7 c7 e7

a9 c9 e9
a10 c10 e10
a11 c11 e11

............



thanks for help

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,236
Default for next loop problems

To answer your first question: Exit For will break out of a For Next loop.

Is GoTo the right way?
If you listen to old school programmers, they'll tell you that GoTo is evil.
Mainly because a guy called Dijkstra said so - he spun some pretty cool code
so you just don't argue with him.

Personally, I'm of the "goto is evil" camp. I try not to use Exit For
either. What you save in coding time you'll make up in bug tracking next
maintenance release.

--
Rob van Gelder - http://www.vangelder.co.nz/excel


"cliee" wrote in message
m...
Hi I am a excel programming beginner, so my question may be stupid

1. how to jump out from a for next loop without error?

eg

For LR = 5 To 25 Step 4
For LC = 1 To 5 Step 1
If ActiveSheet.cells(LR,LC).Value < True Then
Worksheets("Label-s").cells(LR, LC).Value = "abcde"
Worksheets("Label-s").cells(LR+1,LC).Value = "Art no. " & art & " " & des
Worksheets("Label-s").cells(LR+2,LC).Value = con & " " & hen & " " & w
GoTo 178
End If
Next LC
Next LR

I use (goto 178) to go to line 178 is this the right way?
(line 178 is the end of this macro)
-------------------------------------------------


2. I want the follow order for the for next loop

5,9,13,17,21,25
so I use this
For LR = 5 To 25 Step 4

I want 1,3,5
so I use
For LC = 1 To 5 Step 1

the result is that the variable fill in cells I do not wanted such as A8
A13, and b6 .......

the result what I want ( the cells which is fill in something) are like
this....

a5 c5 e5
a6 c6 e6
a7 c7 e7

a9 c9 e9
a10 c10 e10
a11 c11 e11

...........



thanks for help



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default for next loop problems

if you want 1,3,5 then
For LC = 1 To 5 Step 1
should be
For LC = 1 To 5 Step 2

--
Regards,
Tom Ogilvy

"cliee" wrote in message
m...
Hi I am a excel programming beginner, so my question may be stupid

1. how to jump out from a for next loop without error?

eg

For LR = 5 To 25 Step 4
For LC = 1 To 5 Step 1
If ActiveSheet.cells(LR,LC).Value < True Then
Worksheets("Label-s").cells(LR, LC).Value = "abcde"
Worksheets("Label-s").cells(LR+1,LC).Value = "Art no. " & art & " " & des
Worksheets("Label-s").cells(LR+2,LC).Value = con & " " & hen & " " & w
GoTo 178
End If
Next LC
Next LR

I use (goto 178) to go to line 178 is this the right way?
(line 178 is the end of this macro)
-------------------------------------------------


2. I want the follow order for the for next loop

5,9,13,17,21,25
so I use this
For LR = 5 To 25 Step 4

I want 1,3,5
so I use
For LC = 1 To 5 Step 1

the result is that the variable fill in cells I do not wanted such as A8
A13, and b6 .......

the result what I want ( the cells which is fill in something) are like

this....

a5 c5 e5
a6 c6 e6
a7 c7 e7

a9 c9 e9
a10 c10 e10
a11 c11 e11

...........



thanks for help



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
Excel Macro loop problems mibsaweiss Excel Discussion (Misc queries) 0 March 16th 06 04:45 PM
Loop code problems pauluk[_51_] Excel Programming 2 April 23rd 04 10:30 AM
Worksheet_Change - loop within a loop bgm Excel Programming 1 January 19th 04 01:27 PM
HELP!!!! Can't stop a loop (NOT an infinite loop) TBA[_2_] Excel Programming 3 December 14th 03 03:33 PM
If... Then Loop problems in Worksheet Event TB[_3_] Excel Programming 2 August 4th 03 08:45 AM


All times are GMT +1. The time now is 09:28 AM.

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

About Us

"It's about Microsoft Excel"