Compiler Error: Else without If

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Stu120
    New Member
    • Nov 2007
    • 3

    Compiler Error: Else without If

    Hi, I'm trying to code a button to hide and show data in cells H5:I9 (MS Excel 2002)

    Here's the code:

    [CODE=vb]Sub Button21_Click( )
    If Range("H5:H9"). NumberFormat = "General" And Range("I5:I9"). NumberFormat = "0.00%" Then Range("H5:I9"). SelectSelection .NumberFormat = ";;;"
    Else: If Range("H5:I9"). NumberFormat = ";;;" Then Range("H5:I9"). Select
    Selection.Numbe rFormat = "General"
    Range("I5:I9"). Select
    Selection.Numbe rFormat = "0.00%"

    End Sub[/CODE]

    It appears to be ok (messy I know, but valid) yet I get a compiler error saying "Else without If".
    What's wrong? It's driving me nuts!
    Last edited by Killer42; Nov 26 '07, 02:24 AM.
  • daniel aristidou
    Contributor
    • Aug 2007
    • 494

    #2
    Originally posted by Stu120
    Hi, I'm trying to code a button to hide and show data in cells H5:I9 (MS Excel 2002)

    Here's the code:

    Sub Button21_Click( )
    If Range("H5:H9"). NumberFormat = "General" And Range("I5:I9"). NumberFormat = "0.00%" Then Range("H5:I9"). SelectSelection .NumberFormat = ";;;"
    Else: If Range("H5:I9"). NumberFormat = ";;;" Then Range("H5:I9"). Select
    Selection.Numbe rFormat = "General"
    Range("I5:I9"). Select
    Selection.Numbe rFormat = "0.00%"

    End Sub

    It appears to be ok (messy I know, but valid) yet I get a compiler error saying "Else without If".
    What's wrong? It's driving me nuts!
    I believe you missed out 2 end ifs

    TRy adding
    End if
    End if

    Before end sub
    ie like this;
    Code:
    Sub Button21_Click()
    If Range("H5:H9").NumberFormat = "General" And Range("I5:I9").NumberFormat = "0.00%" Then Range("H5:I9").SelectSelection.NumberFormat = ";;;"
    Else: If Range("H5:I9").NumberFormat = ";;;" Then Range("H5:I9").Select
        Selection.NumberFormat = "General"
        Range("I5:I9").Select
        Selection.NumberFormat = "0.00%"
    End if
    End if
    End Sub

    Comment

    • daniel aristidou
      Contributor
      • Aug 2007
      • 494

      #3
      All if statements have to be ended with end if. In your case i belive you had 2 if statments without end ifs

      Comment

      • Stu120
        New Member
        • Nov 2007
        • 3

        #4
        That still doesn't work.
        If it helps I'm trying to link two macros into one, which were recorded as:
        1: To show data -

        [CODE=vb]Sub Show_Calculatio ns()

        Range("H5:I10") .Select
        Selection.Numbe rFormat = "General"
        Range("I5:I9"). Select
        Selection.Numbe rFormat = "0.00%"
        End Sub[/CODE]

        and 2: To hide data -

        [CODE=vb]Sub Hide_Calculatio ns()

        Range("H5:I10") .Select
        Selection.Numbe rFormat = "General"
        Range("I5:I9"). Select
        Selection.Numbe rFormat = "0.00%"
        Range("H5:I9"). Select
        Selection.Numbe rFormat = ";;;"
        End Sub[/CODE]
        Last edited by Killer42; Nov 26 '07, 02:28 AM.

        Comment

        • Ali Rizwan
          Banned
          Contributor
          • Aug 2007
          • 931

          #5
          Originally posted by Stu120
          Hi, I'm trying to code a button to hide and show data in cells H5:I9 (MS Excel 2002)

          Here's the code:

          [CODE=vb]Sub Button21_Click( )
          If Range("H5:H9"). NumberFormat = "General" And Range("I5:I9"). NumberFormat = "0.00%" Then Range("H5:I9"). SelectSelection .NumberFormat = ";;;"
          Else: If Range("H5:I9"). NumberFormat = ";;;" Then Range("H5:I9"). Select
          Selection.Numbe rFormat = "General"
          Range("I5:I9"). Select
          Selection.Numbe rFormat = "0.00%"

          End Sub[/CODE]

          It appears to be ok (messy I know, but valid) yet I get a compiler error saying "Else without If".
          What's wrong? It's driving me nuts!

          Try this code i have made some modifications to it.

          [CODE=vb]
          Sub Button21_Click( )

          If Range("H5:H9"). NumberFormat = "General" And Range("I5:I9"). NumberFormat = "0.00%" Then
          Range("H5:I9"). SelectSelection .NumberFormat = ";;;"
          Else
          If Range("H5:I9"). NumberFormat = ";;;" Then Range("H5:I9"). Select
          Selection.Numbe rFormat = "General"
          Range("I5:I9"). Select
          Selection.Numbe rFormat = "0.00%"

          End If

          End Sub[/CODE]
          Last edited by Killer42; Nov 26 '07, 02:26 AM.

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Stu120, I think what you need to understand here is that VB supports two different structures for the IF statement. One is single-line, the other multi-line.

            The Single-line structure is...
            IF <test> Then <statement> [Else <statement>]

            The multi-line structure is...
            If <test> Then
            <statement> ...
            [Else
            <statement>...]
            End If

            A multi-line If statement must have an End If. A single line If statement cannot have an End If (or a separate Else clause/statement).

            Because you originally wrote the Then on the same line as the If, you made it the single-line version. Thus, the Else which was encountered later didn't belong anywhere.

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              Originally posted by Ali Rizwan
              Try this code ...
              I think you're making things rather confusing by jumbling together the two different IF structures. Try this version...
              [CODE=vb]
              Sub Button21_Click( )

              If Range("H5:H9"). NumberFormat = "General" And Range("I5:I9"). NumberFormat = "0.00%" Then
              Range("H5:I9"). SelectSelection .NumberFormat = ";;;"
              ElseIf Range("H5:I9"). NumberFormat = ";;;" Then
              Range("H5:I9"). Select
              Selection.Numbe rFormat = "General"
              Range("I5:I9"). Select
              Selection.Numbe rFormat = "0.00%"
              End If

              End Sub[/CODE]

              Comment

              • Stu120
                New Member
                • Nov 2007
                • 3

                #8
                Ok that works. Thanks.

                Comment

                Working...