Consulting

Results 1 to 12 of 12

Thread: Why is Next used/necessary

  1. #1

    Why is Next used/necessary

    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

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    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.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    BTW, do you use an Indenter. (see my sig.) This also helps spot missing End ifs, Nexts etc.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    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
    Last edited by Aussiebear; 04-24-2023 at 01:50 AM. Reason: Adjusted the code tags

  5. #5
    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"?

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    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
    Last edited by Aussiebear; 04-24-2023 at 01:51 AM. Reason: Adjusted the code tags
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    I posted your code and ran it to see what would happen.
    So the "Next", is this closing the open "For"?

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    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.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    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.

  10. #10
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    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/Program..._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).




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  11. #11
    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

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •