PDA

View Full Version : [SOLVED] Why is Next used/necessary



YellowLabPro
03-11-2007, 04:51 AM
I am curious as to why "Next c" is used. Let me say for the record that some things are just are. But I like to understand what is going on. In the following Sub, there is an "IF" and is concluded w/ an "End IF", which seems like the way you open and close tags in HTML. But "For" starts a loop, correct? If I understand correctly what is going on here, then why is the "Next c" required?

Thanks,

YLP

mdmackillop
03-11-2007, 04:56 AM
The "c" is not required but it's there for clarity. If you do mix up your Fors and Nexts then by using Next c your code will error allowing you to make the correction. I would advise using it if you have more than one For..Next loop in your code.

mdmackillop
03-11-2007, 04:58 AM
BTW, do you use an Indenter. (see my sig.) This also helps spot missing End ifs, Nexts etc.

YellowLabPro
03-11-2007, 05:00 AM
Forgot to past program:



Dim c As Range, rng
Set rng = Range("a1:a" & Range("a65536").End(xlUp).Row)
For Each c In rng
If c.Row Mod 3 = 0 Then
c.Offset(1, 0).EntireRow.Insert Shift:=xlUp
End If
Next c
End Sub

YellowLabPro
03-11-2007, 05:02 AM
Malcolm,
I do not. I do everything by hand currently. I am trying to develop good habits, and not keeping things simple until it becomes clearer to me. I will check that out.

So, if the "c" is not necessary, is the "Next"?

mdmackillop
03-11-2007, 05:09 AM
Yes, otherwise where would the code loop.

Consider nested loops.

Sub DoLoop()
Dim x As Long, y As Long, z as Long
For z = 1 To 31 Step 10
For x = 3 To 10
For y = 1 To 5
Cells(x + z, y) = "R" & x + z & "C" & y
Next y
Next x
Next z
End Sub

YellowLabPro
03-11-2007, 06:03 AM
I posted your code and ran it to see what would happen.
So the "Next", is this closing the open "For"?

mdmackillop
03-11-2007, 06:08 AM
I've never thought of it as "closing", more like "Go To Next item", but in either meaning, it's necessary to form the loop.

YellowLabPro
03-11-2007, 06:20 AM
Ok, that makes sense.... and good to know this logic.
2 things come to mind that I ponder.
1) As I learn this, it all seems like piece mealing it together, some of this, some of that. I have yet found a clear concise way to learn this subject. Never have done any programming, it is all brand new to me. So I have no background or reference to draw from.
2) Am I going about this the right way??????

One other question,
I accidentally posted the same thing twice, browser was acting up. Is there a way to delete a post? I have searched over and over on the page and the site, but have not been able to locate anything.

malik641
03-11-2007, 11:43 AM
Hey Yelp,

Do you have any programming books? If so, which ones? (Just curious).

I'm not too sure how much this will help, but check out this article:
http://en.wikibooks.org/wiki/Programming:Visual_Basic_Classic/Loops


And I think you're going about this the right way. You should definitely know and understand what's going on with the code that you write (or use from someone's help). That's why were here! So keep asking away and I'm sure all the friendly people here at VBAX will try to help you understand as best as they can (myself included). :thumb

YellowLabPro
03-11-2007, 12:47 PM
Hi Joseph,
I am using VBA for Dummies, by John Walkenbach, currently. I am also cracking the book VBA and Macros, by Jelen.

Any additional advice on readings or resources please feel free to suggest.

regards,

YLP

Bob Phillips
03-11-2007, 12:57 PM
Yelp,

You need to get through John's book before going any further.

Have you taken any programming classes, does you local authority offer such things, as you seem to need to understand the basisc of code flow.