ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Compile Error ElseIf Without If.........But there is an If ! ??? (https://www.excelbanter.com/excel-discussion-misc-queries/171041-compile-error-elseif-without-if-but-there-if.html)

dim

Compile Error ElseIf Without If.........But there is an If ! ???
 
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.

Bob Phillips

Compile Error ElseIf Without If.........But there is an If ! ???
 
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)



"dim" wrote in message
...
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.




Ken Johnson

Compile Error ElseIf Without If.........But there is an If ! ???
 
On Dec 30, 8:27 am, dim wrote:
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

JLGWhiz

Compile Error ElseIf Without If.........But there is an If ! ???
 
"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.

"dim" wrote:

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.


dim

Compile Error ElseIf Without If.........But there is an If ! ?
 
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.

"Ken Johnson" wrote:

On Dec 30, 8:27 am, dim wrote:
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


dim

Compile Error ElseIf Without If.........But there is an If ! ?
 
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. :-)

"JLGWhiz" wrote:

"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.

"dim" wrote:

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.


Bob Phillips

Compile Error ElseIf Without If.........But there is an If ! ?
 
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)



"dim" wrote in message
...
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.

"Ken Johnson" wrote:

On Dec 30, 8:27 am, dim wrote:
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




dim

Compile Error ElseIf Without If.........But there is an If ! ?
 
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.

Robert Baer

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

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.

Robert Baer

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

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.

"Ken Johnson" wrote:


On Dec 30, 8:27 am, dim wrote:

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

Probably limited to compiler input line length.

dim

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

I have it working now. :-D

"Robert Baer" wrote:

dim wrote:

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.


Bob Phillips

Compile Error ElseIf Without If.........But there is an If ! ?
 
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)



"dim" wrote in message
...
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.





All times are GMT +1. The time now is 02:17 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com