Excel User Group
Microsoft Excel blogs, forums, files. Read, ask questions, provide answers.

If / ElseIf / EndIf Structure

Latest post Mon, May 10 2010 9:36 PM by Jim Cone. 1 replies.
  • Mon, May 10 2010 6:23 PM

    • TexasDave
    • Top 50 Contributor
    • Joined on Fri, Jun 26 2009
    • Posts 20
    • Points 340

    If / ElseIf / EndIf Structure

    Quick question, I'm using a If/ElseIf/EndIf structure on a project, and am running into a snag.  I'm getting a "Compile Error - Else without If" when I use it.  I've pasted a portion of the code (all the levels are essentially the same with only slight variations, so I cut out most of it to avoid the dreaded "wall of text").

    Wondering if anyone can see what I've missed here?  I'm self taught, so I'm sure it's probably something blindingly obvious to most folks, so please bear with me!

    Thanks, David

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    Do
    If IsEmpty(ActiveCell) = False Then
            If ActiveCell.Value >= Now() - 366 Then
                Do
                    With Selection.Font
                        .Color = -16776961
                        .TintAndShade = 0
                    End With
                    Selection.Font.Bold = True
                    Selection.Font.Underline = x1underlinestylesingle
            ElseIf ActiveCell.Value >= Now() - 335 Then
                Do
                    With Selection.Font
                        .Color = -4165632
                        .TintAndShade = 0
                    End With
                    Selection.Font.Bold = True
             Else
                Do
                    With Selection.Font
                        .Color = x1Automatic
                        .TintAndShade = 0
                    End With
                ActiveCell.Offset(1, 0).Select
            End If
    Loop Until IsEmpty(ActiveCell) = True
           
    End Sub

    • Post Points: 21
  • Mon, May 10 2010 9:36 PM In reply to

    • Jim Cone
    • Top 10 Contributor
    • Joined on Tue, Jan 15 2008
    • Portland, Oregon - USA
    • Posts 233
    • Points 3,315

    Re: If / ElseIf / EndIf Structure

    The editor counts the number of:   If and End If,  Do and Loop,  For and Next.
    They count for all must be an even number.  However the message displayed when the count is off doesn't always identify the correct missing item(s).
    Also, when you start a Do loop inside an If structure then you must complete the loop inside the If structure  (and vice versa).

    This will work...
    If Sludge Then...
       Do
        'Code
      Loop
    End if

    This will not...
    If Sludge Then...
       Do
        'Code
    End if
      Loop

    You are missing a "Loop" for three "Do" and there should be an "End If" just above "Loop Until IsEmpty(ActiveCell) = True"

    Jim Cone
    Portland, Oregon USA
    ( Special Sort... http://www.contextures.com/excel-sort-addin.html )

     

    • Post Points: 5
Page 1 of 1 (2 items) | RSS
Copyright Excel User Group and the relevant contributors, 2010. All Trade Marks acknowledged. This site is a peer-to-peer site and NOT affiliated in any way to Microsoft Corp. All rights reserved.