Error: Block If without End If

J

jordanbeas

I keep getting the error message "Block If without End If" when I try
to run the following code. Please help!


Sub utahvalleytours()

'define variables
Dim p As Integer
Dim h As Double
Dim ech As Double
Dim ns As Integer
Dim nl As Integer
Dim se As Double
Dim le As Double
Dim sehc As Double
Dim lehc As Double
Dim stp As Double
Dim ltp As Double
Dim tp As Double
Dim sbp As Double
Dim lbp As Double
Dim ehp As Double


'input variables
p = Range("d7").Value
h = Range("d9").Value
sbp = Range("h10").Value
lbp = Range("h11").Value
ehp = Range("h13").Value




If p > 90 Then
ns = 0
nl = 2
If p > 70 Then
ns = 1
nl = 1
If p > 55 Then
ns = 2
nl = 0
If p > 35 Then
ns = 0
nl = 1
Else
ns = 1
nl = 0
End If

'calculate
se = ns * sbp
le = nl * lbp

If h <= 5 Then
sehc = 0
lehc = 0
ech = 0
Else
ech = h - 5
End If

If ech < 4 Then
sehc = ech * ehp * se
lehc = ech * ehp * le
Else
sehc = 4 * ehp * se
lehc = 4 * ehp * le
End If

'compute
stp = se + sehc
ltp = le + lehc
tp = stp + ltp

'output
Range("d14").Value = ech
Range("c18").Value = ns
Range("c20").Value = se
Range("c22").Value = sehc
Range("c24").Value = stp
Range("e18").Value = nl
Range("e20").Value = le
Range("e22").Value = lehc
Range("e24").Value = ltp
Range("d27").Value = tp


End Sub
 
D

Dave Peterson

If p > 90 Then
ns = 0
nl = 2
ElseIf p > 70 Then
ns = 1
nl = 1
ElseIf p > 55 Then
ns = 2
nl = 0
ElseIf p > 35 Then
ns = 0
nl = 1
Else
ns = 1
nl = 0
End If

You may want to look at the "Select Case" statement, too. It may make it a
little easier to understand.

Select case p
case is > 90
ns = 0
nl = 2
case is > 70
ns = 1
nl = 1
case is > 55
ns = 2
nl = 0
case is > 35
ns = 0
nl = 1
case else
ns = 1
nl = 0
End Select

I think it looks a bit cleaner/easier to understand.
 
D

Don Guillett

try it like this or use else if. Look in the help index
'I tested
'If p > 2 Then Cells(1, "g") = 2: Cells(2, "g") = 3

If p > 90 Then ns = 0:nl = 2
If p > 70 Then ns = 1:nl = 1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top