Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / February 2006

Tip: Looking for answers? Try searching our database.

how do i use IF ELSE IF FUNCTION in excel to chk more condition

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
chemnks - 23 Feb 2006 10:01 GMT
i want to check code no(eg. 1,2,3,4,5) in a particular cell and for related
code match i want to display a contents from other cell

Please suggest
K Dales - 23 Feb 2006 12:46 GMT
There are a variety of options, some in Excel Worksheet formulas and some in
VBA.  The easiest is the Worksheet function CHOOSE, e.g:
=CHOOSE(A1,B23,C24,D25)
would display B23 if A1 equals 1, C24 if A1 is 2, or D25 if A1=3 (you could
extend this to allow for your 5 choices).  But this only works if you have
consecutive integers to choose from (or can somehow develop a formula to
generate the integer values).

In VBA, There is also a Choose() function that works similarly.  There is
also Switch() which allows several unrelated conditions, e.g.:

Result = Switch(A=1, B, A=22, B+5, C<45, D)
This gives Result the value B if A=1, or B+5 if A=22, or D if C<45.  It
applies the tests in the order listed so the first one that is true
determines the value (in other words, if A=1 and C<45, Result is B - not D)

Or there is the Select Case Statement:
Select Case Range("A1").Value
Case 1
    Range("B23").Value
Case 2
    Range("C24").Value
etc.
End Select
Signature

- K Dales

> i want to check code no(eg. 1,2,3,4,5) in a particular cell and for related
> code match i want to display a contents from other cell
>
> Please suggest
John Skewes - 23 Feb 2006 13:01 GMT
You use If ElseIf in the following manner:

Sub UseIfElseIf()
     With Range("A1")
           If .Value = 1 Then
                 'do something
           ElseIf .Value = 2 Then
                 'do something else
           ElseIf .Value = 3 Then
                 'do something else again
                 ''
                 ''
                 ''
           End If
     End With
End Sub

However, if there are a large number of ElseIf statements, and if the only
'true' statement is in one of the last ElseIfs's you'll find there's a speed
increase by using Case Statements (they both follow the same logic) e.g.

Sub SelectCase()
     Select Case Range("A1").Value
           Case 1
                 'do something
           Case 2
                 'do something else
           Case 3
                 'do something else again
                 ''
                 ''
                 ''
           End Select
End Sub

HTH,
John
Signature

The major part of getting the right answer lies in asking the right
question...

> i want to check code no(eg. 1,2,3,4,5) in a particular cell and for related
> code match i want to display a contents from other cell
>
> Please suggest
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.