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
 

Cindy Ellis

MrExcel MVP
Joined
Jun 9, 2006
Messages
1,802
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:

Danielle005

New Member
Joined
Jun 24, 2012
Messages
3
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
 

Danielle005

New Member
Joined
Jun 24, 2012
Messages
3
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
 

Cindy Ellis

MrExcel MVP
Joined
Jun 9, 2006
Messages
1,802
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:

Forum statistics

Threads
1,082,342
Messages
5,364,783
Members
400,815
Latest member
gangstar67

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top