Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2008
    Posts
    8

    Unanswered: Compile Error: End if wtihout block if

    HI EVERYONE... I keep getting the error above and can't seem to figure out where my End If's are supposed to be. Please help. Mucho Gracias!!

    sRow = 2

    Range("a1").Select
    If Range("a1").Offset(1, 0).Value <> "" Then
    Selection.End(xlDown).Select
    End If
    eRow = ActiveCell.Row

    For cnt = sRow To eRow

    If Left(Range("a" & cnt).Value, 3) = "Buy" Or Left(Range("a" & cnt).Value, 4) = "Sell" _
    Then Range("A" & cnt).Offset(0, 11).Value = (Range("A" & cnt).Offset(0, 3).Value / 1000000) * Range("A" & cnt).Offset(0, 5).Value
    total = total + Range("A" & cnt).Offset(0, 11).Value
    End If


    If Left(Range("A" & cnt).Value, 5) = " t " Then Range("A" & cnt).Offset(0, 11).Value = total
    End If

    If Left(Range("A" & cnt).Value, 5) = " t " Then Range("A" & cnt).Offset(0, 12).Value = Range("A" & cnt).Offset(0, 11).Value _
    / (Range("A" & cnt).Offset(0, 3).Value / 1000000)

    total = 0


    If Left(Range("A" & cnt).Value, 5) = " t " Then Range("A" & cnt & ":" & "M" & cnt).Font.Bold = True
    If Left(Range("A" & cnt).Value, 5) = " t " Then Range("A" & cnt & ":" & "M" & cnt).Interior.ColorIndex = 6
    End If

    Next cnt


    End Sub

  2. #2
    Join Date
    Dec 2004
    Posts
    37

    If all on one line

    Hi,

    If you put the whole statement on a single line ie

    If AVariable = 10 then DoSomething

    Then there is no need for an "End if"
    This also applies if you use a line continuation with a space and Underscore.


    However if you place a line in it ie

    If AVariable = 10 then
    DoSomething
    End If

    Then there is a requirement for an "End If"

    Hope this helps

  3. #3
    Join Date
    Mar 2008
    Location
    East Anglia
    Posts
    7

    Put some extras end if's in the last few lines - I highlighted them in red

    If Left(Range("A" & cnt).Value, 5) = " t " Then Range("A" & cnt).Offset(0, 12).Value = Range("A" & cnt).Offset(0, 11).Value _
    / (Range("A" & cnt).Offset(0, 3).Value / 1000000)

    total = 0
    END IF

    If Left(Range("A" & cnt).Value, 5) = " t " Then Range("A" & cnt & ":" & "M" & cnt).Font.Bold = True
    END IF
    If Left(Range("A" & cnt).Value, 5) = " t " Then Range("A" & cnt & ":" & "M" & cnt).Interior.ColorIndex = 6
    End If

  4. #4
    Join Date
    Mar 2006
    Posts
    163
    Try this.
    Code:
        sRow = 2
        
        Range("a1").Select
        
        If Range("a1").Offset(1, 0).Value <> "" Then
            Selection.End(xlDown).Select
        End If
        
        eRow = ActiveCell.Row
        
        For cnt = sRow To eRow
        
        If Left(Range("a" & cnt).Value, 3) = "Buy" Or Left(Range("a" & cnt).Value, 4) = "Sell" Then
            Range("A" & cnt).Offset(0, 11).Value = (Range("A" & cnt).Offset(0, 3).Value / 1000000) * Range("A" & cnt).Offset(0, 5).Value
            total = total + Range("A" & cnt).Offset(0, 11).Value
        End If
        
        
        If Left(Range("A" & cnt).Value, 5) = " t " Then
            Range("A" & cnt).Offset(0, 11).Value = total
        End If
        
        If Left(Range("A" & cnt).Value, 5) = " t " Then
            Range("A" & cnt).Offset(0, 12).Value = Range("A" & cnt).Offset(0, 11).Value / (Range("A" & cnt).Offset(0, 3).Value / 1000000)
        
            total = 0
        End If
        
        If Left(Range("A" & cnt).Value, 5) = " t " Then
            Range("A" & cnt & ":" & "M" & cnt).Font.Bold = True
            Range("A" & cnt & ":" & "M" & cnt).Interior.ColorIndex = 6
        End If
        
        Next cnt
    Note as well as sorting the If/End Ifs I've indented the code.

    Doing that makes it far easier to read and far easier to debug.

Posting Permissions

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