+ Reply to Thread
Results 1 to 9 of 9

How do I reset a for next loop in the script?

Hybrid View

  1. #1
    Registered User
    Join Date
    10-15-2012
    Location
    Brawley, California
    MS-Off Ver
    Excel 2010, 2007
    Posts
    11

    Question How do I reset a for next loop in the script?

    Hello friends,

    I have a nested for-next loop inside another for-next loop, but when the nested loop ends it does not reset to the beginning again for the next item in my array loop. I am pretty sure the problem lies in my declarartions and how VBA handles arrays used in For-Next loops. I am having a mental block concerning declaring arrays and their ranges. An example of the setup is below;

    Dim simspg as variant
    Dim simspoint as variant
    simspg = Array("SP001", "SP002", "SP003", "SP004", "SP005", "SP006", "SP007", "SP008", "SP009", "SP010", "SP011", "SP012", "SP013", _
                           "SP014", "SP015", "SP016", "SP017", "SP018", "SP019", "SP020", "SP021")
    For each simspoint in simspg 
    For x = 1 to 100
    Worksheets (simspoint).select
    cells (x,1).value = 12
    next
    next
    How would I properly use these two for-next loops such that "x" is reset to 1 each time a new member of the array is called?

    Thanks for any help, Newbee BAT:-)
    Last edited by Cutter; 10-15-2012 at 05:36 PM. Reason: Added code tags

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: How doo I reset a for next loop in the script?

    Dim simspg as variant
    Dim simspoint as variant
    simspg = Array("SP001", "SP002", "SP003", "SP004", "SP005", "SP006", "SP007", "SP008", "SP009", "SP010", "SP011", "SP012", "SP013", "SP014", "SP015", "SP016", "SP017", "SP018", "SP019", "SP020", "SP021")
    For each simspoint in simspg 
      Worksheets (simspoint).select
      For x = 1 to 100
      cells (x,1).value = 12
      next
    next
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    10-15-2012
    Location
    Brawley, California
    MS-Off Ver
    Excel 2010, 2007
    Posts
    11

    Re: How doo I reset a for next loop in the script?

    I didn't explain due to format control of post window, but the lines are staggered based upon hierarchy of script in the sheets I am working with. The staggering did not help there either. "x" still goes to 100 and does not reset. Thanks though, BAT:-)

  4. #4
    Forum Contributor
    Join Date
    03-15-2007
    Location
    AL, USA
    MS-Off Ver
    2007
    Posts
    174

    Re: How doo I reset a for next loop in the script?

    I'm sure you have a reason for looping through 100, but if you don't, you can simplify your code and resolve the issue by just avoiding it...

    Sub Looper()
    Dim simspg As Variant
    Dim simspoint As Variant
    simspg = Array("SP001", "SP002", "SP003", "SP004", "SP005", "SP006", "SP007", "SP008", "SP009", "SP010", "SP011", "SP012", "SP013", _
        "SP014", "SP015", "SP016", "SP017", "SP018", "SP019", "SP020", "SP021")
    For Each simspoint In simspg
        Worksheets(simspoint).Select
        Range("A1:A100").Value = 12
    Next
    End Sub
    But, if you MUST loop to 100, then try this:
    Sub Looper2()
    Dim simspg As Variant
    Dim simspoint As Variant
    simspg = Array("SP001", "SP002", "SP003", "SP004", "SP005", "SP006", "SP007", "SP008", "SP009", "SP010", "SP011", "SP012", "SP013", _
        "SP014", "SP015", "SP016", "SP017", "SP018", "SP019", "SP020", "SP021")
    For Each simspoint In simspg
        x = 1
        Worksheets(simspoint).Select
        For x = 1 To 100
            Cells(x, 1).Value = 12
        Next
    Next
    End Sub
    Quote Originally Posted by BrianATrease View Post
    I didn't explain due to format control of post window, but the lines are staggered based upon hierarchy of script in the sheets I am working with. The staggering did not help there either. "x" still goes to 100 and does not reset. Thanks though, BAT:-)
    If you had used code tags, your formatting would have remained
    Last edited by mshale; 10-15-2012 at 12:21 PM.

  5. #5
    Registered User
    Join Date
    10-15-2012
    Location
    Brawley, California
    MS-Off Ver
    Excel 2010, 2007
    Posts
    11

    Re: How doo I reset a for next loop in the script?

    I am not really pasting the number 12 to each cell, in all actuallity I am pasting an entire spreadsheet of data from two other sources with calculations and references. Too much to add to this post. just trying to make it simple. As to the second fix, I tried that some time ago and the for next loop still does not reset for some reason (this is confusing since even the cobol and fortran I learned with allowed for reset on re-declaration of the for statement). Thanks though, BAT

  6. #6
    Registered User
    Join Date
    10-15-2012
    Location
    Brawley, California
    MS-Off Ver
    Excel 2010, 2007
    Posts
    11

    Re: How doo I reset a for next loop in the script?

    help on for-next.xlsmIf it helps I have posted the entire spreadsheet with the vba. I am only concerned with the reasoning behind the failed reset of the multiple for-next loops (yes there are several).

    In the Macros list is the file I wanted help with (Macro1) and the eventual fix I used for the problem (Button_3_click). I am not looking for a fix to the problem in my macros, but only an understanding of how to use multiple for-next loops such that they reset.

    I see these as a clock-work where each 60 seconds the next array element (minutes) updates and the for-next "seconds" loop repeats. The only problem I have is how to tell the clock to reset the seconds hand for the next minute.

    I know this is a simple problem and is covered in VBA 101, first week or so, but I am at a loss.

    Please help me understand for future reference. Thanks, Brian Trease

  7. #7
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: How doo I reset a for next loop in the script?

    BrianATrease,

    Welcome to the Excel Forum.

    You do not have to use Select, or loop thru rows 1 to 100, in your macro.


    Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


    
    Option Explicit
    Sub Test()
    ' stanleydgromjr, 10/15/2012
    ' http://www.excelforum.com/excel-programming-vba-macros/868458-how-doo-i-reset-a-for-next-loop-in-the-script.html
    Dim simspg As Variant
    Dim i As Long
    Application.ScreenUpdating = False
    simspg = Array("SP001", "SP002", "SP003", "SP004", "SP005", "SP006", "SP007", "SP008", "SP009", "SP010", "SP011", "SP012", "SP013", _
      "SP014", "SP015", "SP016", "SP017", "SP018", "SP019", "SP020", "SP021")
    For i = LBound(simspg) To UBound(simspg)
      Worksheets(simspg(i)).Cells(1, 1).Resize(100).Value = 12
    Next i
    Application.ScreenUpdating = True
    End Sub

    Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm


    Then run the Test macro.
    Last edited by stanleydgromjr; 10-15-2012 at 12:25 PM.
    Have a great day,
    Stan

    Windows 10, Excel 2007, on a PC.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  8. #8
    Forum Contributor
    Join Date
    03-15-2007
    Location
    AL, USA
    MS-Off Ver
    2007
    Posts
    174

    Re: How do I reset a for next loop in the script?

    I'm not sure if you actually looked at my code in post #4, but it is not the same as the previous suggestions (if that is what you were referring to by "tried that some time ago"). I will post a sample from your code that has the same methods as mine in post #4:
        For Each samploc1 In simspg1
            x = 2
            For x = 2 To 10
            If Cells(x, 14).Value = samploc1 Then GoSub populate
            dup1 = Cells(x, 14).Value
            Next
        Next
    Notice the position of x = 2. This will reset x to 2 with each iteration of samploc1.

  9. #9
    Registered User
    Join Date
    10-15-2012
    Location
    Brawley, California
    MS-Off Ver
    Excel 2010, 2007
    Posts
    11

    Re: How do I reset a for next loop in the script?

    All apologies, and yes it does work quite well now.

    I did indeed try the reset value before the for statement some time ago, but I believe the "Next" statements were in the wrong order and caused the failure of the count. I kept thinking there was some unwritten rule for resetting the count. Its just me trying to make things difficult;-).

    Thanks and KUDOS to you for all your help! Hail to mshale!

    BAT :-)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1