none
Do/For Loops in Excel 2013 VBA RRS feed

  • Question

  • Though I was a developer for many years, I'm very new to VBA and I'm having a compiler problem with Do/For loops.  I'm getting an error stating that I have a Loop without a Do or a Next without a For (I've tried both ways).  As far as I can see, there are the same number of Do's/For's as there are Loop/Next's.  Is there anything inside the loop that would stop the compiler from seeing it? 

    Here's the code I'm struggling with:

        Do Until ScoreCardNum > 7
            Do Until MatchNum > 2
                If Range("d9").Offset(StartRow, 0).Value < Range("d9").Offset(StartRow + 1, 0).Value Then
                    HandicapDiff = Range("d9").Offset(StartRow + 1, 0).Value - Range("d9").Offset(StartRow, 0).Value
                    StartCol = StartCol + 1
                ElseIf Range("d9").Offset(StartRow, 0).Value > Range("d9").Offset(StartRow + 1, 0).Value Then
                    HandicapDiff = Range("d9").Offset(StartRow, 0).Value - Range("d9").Offset(StartRow + 1, 0).Value
                End If
                If HandicapDiff > 0 Then
                    Do Until HoleNum > 9
                        If Player1 > 18 Then
                            If Range("f3").Offset(0, StartCol).Value > Range("f9").Offset(StartRow, StartCol).Value Then
                                Range("f9").Offset(StartRow, StartCol).BackColor.rgbLightRed
                                Player1 = Player1 - 1
                                StartCol = StartCol + 1
                            End If
                        ElseIf Player1 > 9 Then
                            If Range("f3").Offset(0, StartCol).Value > Range("f9").Offset(StartRow, StartCol).Value Then
                                Range("f9").Offset(StartRow, StartCol).BackColor.rgbLightBlue
                                Player1 = Player1 - 1
                                StartCol = StartCol + 1
                            End If
                        ElseIf Player1 > 0 Then
                            If Range("f3").Offset(0, StartCol).Value > Range("f9").Offset(StartRow, StartCol).Value Then
                                Range("f9").Offset(StartRow, StartCol).BackColor.rgbLightBlue
                                Player1 = Player1 - 1
                                StartCol = StartCol + 1
                            End If
                        End If
                        HoleNum = HoleNum + 1
                   Loop
                If StartRow < 2 Then
                    StartRow = 4
                Else
                    StartRow = 1
                End If
                StartCol = 0
                MatchNum = MatchNum + 1
                Loop  (THIS IS THE STATEMENT THAT IS HIGHLIGHTED WHEN I GET THE ERROR)
            MatchNum = 1
            StartRow = StartRow + 19
            HandicapDiff = 0
            HighlightRow = 0
            HighlightColor = 1
            ScoreCardNum = ScoreCardNum + 1
       Loop

    Thanks in advance,

    Vinny

    Tuesday, August 19, 2014 2:02 PM

Answers

  • The error message and highlight are misleading.

    You're missing one End If, just above the line If StartRow < 2 Then. It is the End If belonging to the line If HandicapDiff > 0 Then


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Proposed as answer by George HuaModerator Wednesday, August 20, 2014 6:55 AM
    • Marked as answer by Vpp1253 Monday, August 25, 2014 1:09 PM
    Tuesday, August 19, 2014 2:46 PM