ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   IF THEN ELSEIF - newbie needs help (https://www.excelbanter.com/excel-programming/328446-if-then-elseif-newbie-needs-help.html)

[email protected]

IF THEN ELSEIF - newbie needs help
 
Hi all

I am trying to learn as I go and desperately hoping someone can help
me...First I will explain what I want the program to do, then I will
paste my code....(which is not working : ))

What I want is this: If Cell A2 contains the value 2, and cell e5 is
null, then cell A15 should contain nothing.

But...if cell a2 contains 2 and cell e5 is greater than 84999 or less
than 12000, cell A15 should display "Error"

This will go on for about 4 or 5 cells (e6, e7, e8, etc.) The first
part of it is ok, I am having trouble with the ElseIf. Everytime I
think I get it (and I have looked at some samples on the web), it is
not right and I can't seem to figure out what I am doing wrong. Would
appreciate anyone helping...I think I should say that I would not ever
consider asking anyone to write code for me...i know the value of that
knowledge...but I am trying to teach myself and just would like some
guidance....Thanks!

This is my code:

Sub Test2()
If Range("a1") = 2 Then
If Range("e5") = "" Then Range("a15") = ""
ElseIf Range("e5") 849999 Then Range("a15") = "Error"
Else: Range("a15") = ""
End If
ElseIf Range("e5") < 120000 Then Range("a15") = "Error"
Else: Range("a15") = ""
End If
End Sub


Bob Phillips[_6_]

IF THEN ELSEIF - newbie needs help
 
This works for me

If Range("a1") = 2 Then
If Range("e5") = "" Then
Range("a15") = ""
ElseIf (Range("e5") 849999 Or _
Range("e5") < 120000) Then
Range("a15") = "Error"
Else
Range("a15") = ""
End If
End If


--

HTH

RP
(remove nothere from the email address if mailing direct)


wrote in message
oups.com...
Hi all

I am trying to learn as I go and desperately hoping someone can help
me...First I will explain what I want the program to do, then I will
paste my code....(which is not working : ))

What I want is this: If Cell A2 contains the value 2, and cell e5 is
null, then cell A15 should contain nothing.

But...if cell a2 contains 2 and cell e5 is greater than 84999 or less
than 12000, cell A15 should display "Error"

This will go on for about 4 or 5 cells (e6, e7, e8, etc.) The first
part of it is ok, I am having trouble with the ElseIf. Everytime I
think I get it (and I have looked at some samples on the web), it is
not right and I can't seem to figure out what I am doing wrong. Would
appreciate anyone helping...I think I should say that I would not ever
consider asking anyone to write code for me...i know the value of that
knowledge...but I am trying to teach myself and just would like some
guidance....Thanks!

This is my code:

Sub Test2()
If Range("a1") = 2 Then
If Range("e5") = "" Then Range("a15") = ""
ElseIf Range("e5") 849999 Then Range("a15") = "Error"
Else: Range("a15") = ""
End If
ElseIf Range("e5") < 120000 Then Range("a15") = "Error"
Else: Range("a15") = ""
End If
End Sub




Nick

IF THEN ELSEIF - newbie needs help
 
Maybe I've missed the point but why aren't you just using the IF worksheet
function in the cells on the worksheet ??

Nick
wrote in message
oups.com...
Hi all

I am trying to learn as I go and desperately hoping someone can help
me...First I will explain what I want the program to do, then I will
paste my code....(which is not working : ))

What I want is this: If Cell A2 contains the value 2, and cell e5 is
null, then cell A15 should contain nothing.

But...if cell a2 contains 2 and cell e5 is greater than 84999 or less
than 12000, cell A15 should display "Error"

This will go on for about 4 or 5 cells (e6, e7, e8, etc.) The first
part of it is ok, I am having trouble with the ElseIf. Everytime I
think I get it (and I have looked at some samples on the web), it is
not right and I can't seem to figure out what I am doing wrong. Would
appreciate anyone helping...I think I should say that I would not ever
consider asking anyone to write code for me...i know the value of that
knowledge...but I am trying to teach myself and just would like some
guidance....Thanks!

This is my code:

Sub Test2()
If Range("a1") = 2 Then
If Range("e5") = "" Then Range("a15") = ""
ElseIf Range("e5") 849999 Then Range("a15") = "Error"
Else: Range("a15") = ""
End If
ElseIf Range("e5") < 120000 Then Range("a15") = "Error"
Else: Range("a15") = ""
End If
End Sub




[email protected]

IF THEN ELSEIF - newbie needs help
 
I should have mentioned this....my mistake - we have used formulas for
some of these calculations, but in this case, we will have more than 7
statements....


Nick

IF THEN ELSEIF - newbie needs help
 
I think this would be much better as a custom function and then you could
copy it down the cells.
When you say this goes on for several rows is the comparison always to cell
C2 or is it C3, C4...etc?

Nick

wrote in message
oups.com...
I should have mentioned this....my mistake - we have used formulas for
some of these calculations, but in this case, we will have more than 7
statements....




Tom Ogilvy

IF THEN ELSEIF - newbie needs help
 
You can also have more than 7 conditions if you do it correctly.

=if(1st condition, if(),if())

do if you can divide you work in half with one condition, then each of the
options can have 6 conditions for example.

but it appears like you are using a table type situation. If so, look at
Vlookup or one of the other lookup functions.

--
Regards,
Tom Ogilvy

wrote in message
oups.com...
I should have mentioned this....my mistake - we have used formulas for
some of these calculations, but in this case, we will have more than 7
statements....




[email protected]

IF THEN ELSEIF - newbie needs help
 
It would be to cells C3, C4, C5, up to about 20 rows.


JTF

IF THEN ELSEIF - newbie needs help
 
Bob

Thanks! This is working a test group of cells. More importantly, I
think I finally understand *how* it works. Thanks again



All times are GMT +1. The time now is 10:37 PM.

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