+ Reply to Thread
Results 1 to 3 of 3

Nesting a loop within a loop error

Hybrid View

  1. #1
    Registered User
    Join Date
    12-22-2010
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    16

    Nesting a loop within a loop error

    I am trying to run a loop within a loop... i need 1 varaible to be satisfied so that then the main loop (nested one) starts and when its done the macro goes to the next cell down (z + 1) and performs the same loops there.

    When I try this i get the following error occurs on the last line of the code:

    Compile Error: Loop without Do

    any idea how to fix this?

    Heres the code:
    Sub Calendar_Calc_Avgs()
    
    Dim y As Long
    Dim z As Long
    Dim avgM
    Dim countM As Long
    
    y = 0
    z = 20
    avgM = 0
    countM = 0
    
    Do
    
    If IsEmpty(Sheets("Averages").Range("B" & z)) = False Then
    
    With Sheets("Calendar").Range("G4")
    
        Do
    
        If .Offset(y, 0) = Sheets("Averages").Range("B" & z) Then
             avgM = avgM + .Offset(y, -2)
             countM = countM + 1
        End If
    
        y = y + 1
        Loop Until .Offset(y, 1) = 1
    
    End With
    
    Sheets("Averages").Range("C" & z).Value = avgM / countM
    
    z = z + 1
    
    Loop Until Sheets("Averages").Range("C" & z).Offset(0, -1) = ""
    
    End Sub

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,645

    Re: Nesting a loop within a loop error

    Hi there,

    I think you've omitted an "End If" statement. Try the following code and see if it works:



    Option Explicit
    
    
    Sub Calendar_Calc_Avgs()
    
        Dim countM  As Long
        Dim y       As Long
        Dim z       As Long
        Dim avgM
    
        y = 0
        z = 20
        avgM = 0
        countM = 0
    
        Do
    
            If IsEmpty(Sheets("Averages").Range("B" & z)) = False Then
    
                With Sheets("Calendar").Range("G4")
    
                    Do
    
                        If .Offset(y, 0) = Sheets("Averages").Range("B" & z) Then
                             avgM = avgM + .Offset(y, -2)
                             countM = countM + 1
                        End If
    
                    y = y + 1
    
                    Loop Until .Offset(y, 1) = 1
    
                End With
    
            End If          '   THIS STATEMENT WAS OMITTED FROM YOUR ORIGINAL CODE
    
            Sheets("Averages").Range("C" & z).Value = avgM / countM
    
            z = z + 1
    
        Loop Until Sheets("Averages").Range("C" & z).Offset(0, -1) = ""
    
    End Sub


    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Nesting a loop within a loop error

    Learn to indent your code properly and you'll spot missing END IF spots:
    Option Explicit
    
    Sub Calendar_Calc_Avgs()
    Dim y As Long
    Dim z As Long
    Dim avgM
    Dim countM As Long
    
    y = 0
    z = 20
    avgM = 0
    countM = 0
    
    Do
        If IsEmpty(Sheets("Averages").Range("B" & z)) = False Then
            With Sheets("Calendar").Range("G4")
                Do
                    If .Offset(y, 0) = Sheets("Averages").Range("B" & z) Then
                         avgM = avgM + .Offset(y, -2)
                         countM = countM + 1
                    End If
                    y = y + 1
                Loop Until .Offset(y, 1) = 1
            End With
            Sheets("Averages").Range("C" & z).Value = avgM / countM
            z = z + 1
        End If
    Loop Until Sheets("Averages").Range("C" & z).Offset(0, -1) = ""
    
    End Sub

    EDIT: Hat-tip to Greg.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

+ 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