Compile error "Else without if"

M

Morlin

Help.

I had a userform that I can fill out and then save to a spreadsheet.
Then I can Look up the information and have it put back into a
spreadsheet. To make changes. Then it is necessary to save the
changes again.

If CheckMilo Then Cells(NextRow, 8) = "2"
Else
Cells(NextRow, 8) = ""
End If

this is one of about 25-30 check boxes I need to be able to affect. I
get an error Compile error "else without if" could somebody tell me
how to right it so I don't get the error.
 
M

Mike H

Hi

Without seeing the rest of the code I have no idea if the code works but
this should make it compile

If CheckMilo Then
Cells(NextRow, 8) = "2"
Else
Cells(NextRow, 8) = ""
End If

Mike
 
M

Morlin

I gotta ask. How is that different from what I had? do you have to
have them on seperate lines like that so instead of 4 lines of code
that I had it needs to be 5?

Thanks for the answer. it worked I just need to know if that is
why.
 
D

Dave Peterson

It's the difference between a single line if/then/else statement and a block
if/end/else statement.

If CheckMilo Then Cells(NextRow, 8) = 2 Else Cells(NextRow, 8) = ""

Would have worked ok. But for me, I find it miserable to read--just too easy to
miss something important.

I do my best to use the block if statement:

If CheckMilo Then
Cells(NextRow, 8) = "2"
Else
Cells(NextRow, 8) = ""
end if

I think it's easier to read and update.
 

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