Compile Error: Else without If, Expected: Expression AND End If without block If

Danielle005

New Member
Joined
Jun 24, 2012
Messages
3
Hi!

This is my first post so I apologise in advance if I do something wrong!
I am very much a novice VBA user. I've been teaching it to myself so I can use it for my honours year thesis.
I'm not sure if I have done something ridiculous here but I've included my code so you all can see. I keep getting multiple errors with this code. One of these is the Compile Error: Else without If and yet there is definitely an If for the Else! I also get another compile error: Expected: Expression. And one more Compile Error: End If without block If
What I am trying to do if build in an error
Any help would be much appreciated! :)

If Pref(Lp) = 1 Then _
Do
x(tgt) = InputBox(Str8 & Preferred & " to a " & NotPreferred & "." & vbNewLine & vbNewLine & Str12 & Str13 & Str14)
If x(tgt) < geom Or x(tgt) > x(y(Lp, 2)) Then _
MsgBox ("Your answer is invalid. Please ensure that you enter a value between" & geom & " and " & x(y(Lp, 2)))
Loop Until x(tgt) > geom And x(tgt) < x(y(Lp, 2))
Else
Do
x(tgt) = InputBox(Str8 & Preferred & " to a " & NotPreferred & "." & vbNewLine & vbNewLine & Str12 & Str13 & Str14)
If x(tgt) < x(y(Lp, 1)) Or x(tgt) > geom Then
MsgBox ("Your answer is invalid. Please ensure that you enter a value between" & x(y(Lp, 1)) & " and " & geom)
Loop Until x(tgt) > x(y(Lp, 1)) And x(tgt) < geom
End If
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Hello, and Welcome to the forum!
Since I don't know what the inputs are to this code, it's difficult to test, so I'm not sure my answer will solve all of the problems you've had with it. That said...
First, remove the trailing "_" from all the lines that end with "Then _". The underscore at the end tells Excel it's a continuation of the same line, not a "Block If".
Give that a try, then post back with the outcome if that doesn't resolve it.

Code:
[COLOR=#333333]If Pref(Lp) = 1 Then [/COLOR]
[COLOR=#333333]Do[/COLOR]
[COLOR=#333333]x(tgt) = InputBox(Str8 & Preferred & " to a " & NotPreferred & "." & vbNewLine & vbNewLine & Str12 & Str13 & Str14)[/COLOR]
[COLOR=#333333]If x(tgt) < geom Or x(tgt) > x(y(Lp, 2)) Then [/COLOR]
[COLOR=#333333]MsgBox ("Your answer is invalid. Please ensure that you enter a value between" & geom & " and " & x(y(Lp, 2)))[/COLOR]
[COLOR=#333333]Loop Until x(tgt) > geom And x(tgt) < x(y(Lp, 2))[/COLOR]
[COLOR=#333333]Else[/COLOR]
[COLOR=#333333]Do[/COLOR]
[COLOR=#333333]x(tgt) = InputBox(Str8 & Preferred & " to a " & NotPreferred & "." & vbNewLine & vbNewLine & Str12 & Str13 & Str14)[/COLOR]
[COLOR=#333333]If x(tgt) < x(y(Lp, 1)) Or x(tgt) > geom Then[/COLOR]
[COLOR=#333333]MsgBox ("Your answer is invalid. Please ensure that you enter a value between" & x(y(Lp, 1)) & " and " & geom)[/COLOR]
[COLOR=#333333]Loop Until x(tgt) > x(y(Lp, 1)) And x(tgt) < geom[/COLOR]
[COLOR=#333333]End If[/COLOR]

Hope that helps,
 
Last edited:
Upvote 0
Thank you for your help!! I have another question - to do with the same code. x(tgt) is As Integer (in terms of dimension). I'm trying to add code into to the above code, so that if the user enters a non-numeric value then the message box pops up as well. If I change it as follows:
Do
x(tgt) = InputBox(Str8 & Preferred & " to a " & NotPreferred & "." & vbNewLine & vbNewLine & Str12 & Str13 & Str14)
If Not IsNumeric(x(tgt)) Or x(tgt) < geom Or x(tgt) > x(y(Lp, 2)) Then
MsgBox ("Your answer is invalid. Please ensure that you enter a value between " & Format(Str(geom) / 100, "#%") & _
" and " & Format(Str(x(y(Lp, 2)) / 100), "#%"))
End If
Loop Until IsNumeric(x(tgt)) And x(tgt) > geom And x(tgt) < x(y(Lp, 2))
Else
Do
x(tgt) = InputBox(Str8 & Preferred & " to a " & NotPreferred & "." & vbNewLine & vbNewLine & Str12 & Str13 & Str14)
If Not IsNumeric(x(tgt)) Or x(tgt) < x(y(Lp, 1)) Or x(tgt) > geom Then
MsgBox ("Your answer is invalid. Please ensure that you enter a value between " & Format(Str(x(y(Lp, 1)) / 100), "#%") _
& " and " & Format(Str(geom) / 100, "#%"))
End If
Loop Until IsNumeric(x(tgt)) And x(tgt) > x(y(Lp, 1)) And x(tgt) < geom
End If

Nothing happens! When the user enters text in the input box the whole code crashes. I thought I could fix this. This is forming part of an experiment, so I need to make sure that the user can't by mistake enter a non-numerical value and then all their responses are lost when the system crashes! It'll frustrate them and they will be volunteers so they have every right to walk out and I'd prefer they didn't without completing the survey first!! Please help? Thank you again
 
Upvote 0
Thank you for your help!! I have another question - to do with the same code. x(tgt) is As Integer (in terms of dimension). I'm trying to add code into to the above code, so that if the user enters a non-numeric value then the message box pops up as well. If I change it as follows:
Do
x(tgt) = InputBox(Str8 & Preferred & " to a " & NotPreferred & "." & vbNewLine & vbNewLine & Str12 & Str13 & Str14)
If Not IsNumeric(x(tgt)) Or x(tgt) < geom Or x(tgt) > x(y(Lp, 2)) Then
MsgBox ("Your answer is invalid. Please ensure that you enter a value between " & Format(Str(geom) / 100, "#%") & _
" and " & Format(Str(x(y(Lp, 2)) / 100), "#%"))
End If
Loop Until IsNumeric(x(tgt)) And x(tgt) > geom And x(tgt) < x(y(Lp, 2))
Else
Do
x(tgt) = InputBox(Str8 & Preferred & " to a " & NotPreferred & "." & vbNewLine & vbNewLine & Str12 & Str13 & Str14)
If Not IsNumeric(x(tgt)) Or x(tgt) < x(y(Lp, 1)) Or x(tgt) > geom Then
MsgBox ("Your answer is invalid. Please ensure that you enter a value between " & Format(Str(x(y(Lp, 1)) / 100), "#%") _
& " and " & Format(Str(geom) / 100, "#%"))
End If
Loop Until IsNumeric(x(tgt)) And x(tgt) > x(y(Lp, 1)) And x(tgt) < geom
End If

Nothing happens! When the user enters text in the input box the whole code crashes. I thought I could fix this. This is forming part of an experiment, so I need to make sure that the user can't by mistake enter a non-numerical value and then all their responses are lost when the system crashes! It'll frustrate them and they will be volunteers so they have every right to walk out and I'd prefer they didn't without completing the survey first!! Please help? Thank you again
 
Upvote 0
I'm not really sure what you mean by "the whole code crashes". Does it display an error message? If so, which line is highlighted if you choose "debug"?
Assuming that it displays a "type mismatch" error at the line
Code:
[COLOR=#574123]x(tgt) = InputBox(Str8 & Preferred & " to a " & NotPreferred & "." & vbNewLine & vbNewLine & Str12 & Str13 & Str14)[/COLOR]
you can get past that point by converting the response to a value. If the response is text, it will be converted to 0, which should simplify the comparison. You would still have problems with the "If" statement though. Assuming that I understand what your code should do, those 2 lines should be changed to:
Code:
[COLOR=#574123]x(tgt) = val(InputBox(Str8 & Preferred & " to a " & NotPreferred & "." & vbNewLine & vbNewLine & Str12 & Str13 & Str14))
[/COLOR][COLOR=#574123]If  x(tgt) < geom Or x(tgt) > x(y(Lp, 2)) Then[/COLOR]
If zero is an acceptable response, then this won't work, as a response of 0 won't be distinguished from a text entry. So, if that's the case, change the declaration of x(tgt) to variant (instead of integer), and change the code as follows:
Code:
[COLOR=#574123]x(tgt) = InputBox(Str8 & Preferred & " to a " & NotPreferred & "." & vbNewLine & vbNewLine & Str12 & Str13 & Str14)
[/COLOR][COLOR=#574123]If  not(isnumeric(x(tgt))) Or x(tgt) < geom Or x(tgt) > x(y(Lp, 2)) Then[/COLOR]
I've tested these on a modified version of your code that did not require all the other input variables, but I couldn't test your code exactly due to lack of context.

Hope that helps,
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,561
Messages
6,114,317
Members
448,564
Latest member
ED38

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top