PDA

View Full Version : Compile Error Regarding If Statement



harber95
07-21-2015, 02:32 PM
After using the code:

Private Sub Concentration_Change() 'concentration
Dim a As Double
Dim s As String
If a < 0 Then
MsgBox "please use a number that applies to: 80>=x>=2.4"
If 0 <= a < 2.4 Then
MsgBox "Please use a higher number"
If a > 80 Then
MsgBox "Please use a smaller number"
If a = s Then
MsgBox "Please use a number that applies to: 80>=x>=2.4"
End If
End Sub

I receive this error msg: "Compile Error: Block If without End If".

What should I do?

Paul_Hossler
07-21-2015, 04:56 PM
If statements used like that each need an EndIf

I indented 4 examples of different ways. But had to use pictures since sometimes the [ CODE ] tag gets my indenting messed up and I wanted to make sure that the structure was clear

I suspect that you want to use the second construct with the If / ElseIf / End If

harber95
07-21-2015, 08:35 PM
Thanks

Aflatoon
07-22-2015, 01:01 AM
BTW, this doesn't do what you think:

If 0 <= a < 2.4 Then

It actually evaluates (0 <= a) to True or False and then compares that True/False to see if it's less than 2.4 - which it always will be, since True/False evaluate to -1/0 respectively in VBA. You need:

If 0 <= a And a < 2.4 Then
or use a Select Case approach.

harber95
07-22-2015, 03:13 AM
thanks