ElseIf


Posted by Bill Muirhead on July 16, 2001 12:05 PM

Can anyone tell me why the following code results in the error Compile Error: Else Without If? As far as I can tell it should work, but it doesn't.

JN = TextBox1.Text
Cells(NextRow, 7) = JN
If JN <> "" Then Cells(NextRow, 6) = "Y"
ElseIf JN = "" Then Cells(NextRow, 6) = "N"
End If

I have a text box on a form and I am transferring the contents to the next empty row at column 6. If there is no JN then I want to place N in the previous cell. If there is a JN then I want to place Y in the previous cell.

I know the answer must be simple but it eludes me.

Thanks for your help.

Bill M

Posted by Gregc on July 16, 2001 12:30 PM

JN = TextBox1.Text
Cells(NextRow, 7) = JN
If JN <> "" Then
Cells(NextRow, 6) = "Y"
ElseIf JN = "" Then
Cells(NextRow, 6) = "N"
End If



Posted by dkh on July 16, 2001 1:33 PM

The if statement has two flavors, single line and block. You get an error because you are trying to use block syntax but VB is thinking you are using single line syntax.

This line is a complete if-then-endif statement and will work just as it is. Endif is not required when using single line syntax. When you follow this up with:

you get an error because VB isn't in an if loop anymore. Use the block syntax as gregc suggests.