Compile Error ElseIf Without If.........But there is an If ! ???

  • Thread starter Thread starter dim
  • Start date Start date
D

dim

Hi folks,

Can someone please try and debug this code for me? ARGHHHH!! I just don't
get it, why am I getting the compile error, there is obviously an 'If' so why
is it saying there's not?

If (Range("D9").Value) < 0 Then Range("D9").Select: Selection.ClearContents:
UserForm6.Show
ElseIf (Range("D9").Value) > 125 Then Range("D9").Select:
Selection.ClearContents: UserForm6.Show
End If

Thankyou for any advice.
 
When you have the action on the same line as the If , there is no closing
End If required. As you have Else clauses, you need to structre it like so

If (Range("D9").Value) < 0 Then
Range("D9").ClearContents:
UserForm6.Show
ElseIf (Range("D9").Value) > 125 Then
Range("D9").ClearContents
UserForm6.Show
End If


--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
Hi folks,

Can someone please try and debug this code for me? ARGHHHH!! I just don't
get it, why am I getting the compile error, there is obviously an 'If' so why
is it saying there's not?

If (Range("D9").Value) < 0 Then Range("D9").Select: Selection.ClearContents:
UserForm6.Show
ElseIf (Range("D9").Value) > 125 Then Range("D9").Select:
Selection.ClearContents: UserForm6.Show
End If

Thankyou for any advice.

If Range("D9").Value < 0 Or Range("D9").Value > 125 Then
Range("D9").ClearContents: UserForm6.Show
End If

Ken Johnson
 
"If" in VBA is not treated as a function and therefor does not require the
parentheses in the code structure. You can also omit the use of colons by
simply moving the next executable command to the next line. Ken Johnson has
eliminated all the unecessary code from you original code structure to
illustrate how it can be made more efficient with less detail.
 
Thanks Bob, your helping me out a lot lately! :-) And Ken I appreciate the
variation, I had'nt come across the 'Or' ability yet and it'll be very handy.

So one more question stemming from that....:rolleyes:....how many of the
'Or' can I use in a single line statement?
If...< 0 Or Range...>125 Or Range...<24 Or Range....>82 Or Range < 6 etc ...
Then...

Thanks again.
 
Thankyou JLGWhiz also for the explanation. I hadn't seen your post before my
last reply!
Again any info regarding limits on the number of [Or]'s is appreciated. :-)
 
I am not sure on the absolute limit, but I would guess that it is far more
than you would (should?) ever need.

But be wary with multiple ORs, you need to ensure that they are well
constructed. For instance in your example is 2 valid? You say

< 0 whereby 2 is not valid, and then add
< 24 whereby 2 is valid

Maybe you mean something like (just as an example)

IF (Range > 4 AND Range <6) OR (Range >20 AND Range < 24) OR ... etc.

just pointing out some potential pit-falls.


--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
Thanks Bob,

I quickly typed my example, it wasn't written to be valid, just to
illustrate the question. I should have written something like.....= 0 Or....=
4 Or....= 14 etc etc. Either way, you've answered my question with the answer
I was hoping for! :-) Thanks again. L8rs.
 
dim said:
Hi folks,

Can someone please try and debug this code for me? ARGHHHH!! I just don't
get it, why am I getting the compile error, there is obviously an 'If' so why
is it saying there's not?

If (Range("D9").Value) < 0 Then Range("D9").Select: Selection.ClearContents:
UserForm6.Show
ElseIf (Range("D9").Value) > 125 Then Range("D9").Select:
Selection.ClearContents: UserForm6.Show
End If

Thankyou for any advice.
Incmplete code...the outer IF has no endif and maybe the compiler is
looking for an elseif in the absence of that endif.
 
dim said:
Thanks Bob, your helping me out a lot lately! :-) And Ken I appreciate the
variation, I had'nt come across the 'Or' ability yet and it'll be very handy.

So one more question stemming from that....:rolleyes:....how many of the
'Or' can I use in a single line statement?
If...< 0 Or Range...>125 Or Range...<24 Or Range....>82 Or Range < 6 etc ...
Then...

Thanks again.

:
Probably limited to compiler input line length.
 
Thanks folks,

I have it working now. :-D

Robert Baer said:
Incmplete code...the outer IF has no endif and maybe the compiler is
looking for an elseif in the absence of that endif.
 
I thought that might be the case, but thought it best to point out the
potential pit-falls just in case.

Have a great 2008!

--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
Back
Top