Block if without end if - VBA Error
This is a small snippet of the code I am writing. It seems to throw an "BLOCK IF WITHOUT END IF" error on the last line. I can't spot an error here, after consulted with this MSDN link
If Longs > 10 & Shorts > 10 Then
If Longs < Shorts Then
Pairs = Longs
Else
Pairs = Shorts
End If
Else
If Longs < 10 & Shorts > 10 Then
Shortfall = True
Pairs = 10
Else: Shortfall = False
Pairs = 10
End If
End Sub
Any help would be highly appre开发者_StackOverflowciated
Change your
Else
If Longs < 10 & Shorts > 10 Then
to
ElseIf Longs < 10 & Shorts > 10 Then
That way you're not unnecessarily nesting If
constructs, but just adding an extra condition to your outer If
construct.
Like this:
If Longs > 10 & Shorts > 10 Then
If Longs < Shorts Then
Pairs = Longs
Else
Pairs = Shorts
End If
ElseIf Longs < 10 & Shorts > 10 Then
Shortfall = True
Pairs = 10
Else
Shortfall = False
Pairs = 10
End If
Heck, you can even leave this intact, even though it's quite ugly:
Else: Shortfall = False
Pairs = 10
You've got 3 If
but only 2 End If
, change the End Sub
at the end to End If
.
If you indent the code properly, it'll be much easier to see:
If Longs > 10 & Shorts > 10 Then
If Longs < Shorts Then
Pairs = Longs
Else
Pairs = Shorts
End If
Else
If Longs < 10 & Shorts > 10 Then
Shortfall = True
Pairs = 10
Else
Shortfall = False
Pairs = 10
End If
' missing End If
End Sub
Change
Else: Shortfall = False
into
Else
Shortfall = False
Pairs = 10
end if
精彩评论