Basic If, Then, ElseIf, Else question

User Name

Spammer
Joined
Aug 10, 2010
Messages
182
If and ElseIfs have always given me fits. I have a couple of other places in my code where I use If, ElseIf, Else constructions. They seem to work. This one doesn't for some reason. Do I have a spacing problem? Any ideas?

PHP:
'This section of code calculates the incidence angle modifier (Kta)
INCAN = INCAN * Rad2Deg
    
If INCAN < 7 Then Kta = 1
ElseIf INCAN < 12 Then Kta = 0.9999
ElseIf INCAN < 15 Then Kta = 0.9998
ElseIf INCAN < 90 Then Kta = 0.01654 + (90 - INCAN) * 0.13686
Else: Kta = 0.01654
End If

INCAN = INCAN * Deg2Rad
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
It automatically puts the semi-colon if you put everything on one line. But you are essentially correct. I had a spacing problem. Thank you.

HTML:
'This section of code calculates the incidence angle modifier (Kta) 
INCAN = INCAN * Rad2Deg 
     
If INCAN < 7 Then 
Kta = 1 
ElseIf INCAN < 12 Then 
Kta = 0.9999 
ElseIf INCAN < 15 Then 
Kta = 0.9998 
ElseIf INCAN < 90 Then 
Kta = 0.01654 + (90 - INCAN) * 0.13686 
Else
Kta = 0.01654 
End If 

INCAN = INCAN * Deg2Rad
 
Last edited:
Upvote 0
You coded a simple IF statement and then added the back end of a block IF statement. Have this:-

How to code IF statements

1) Simple IF with a single statement
Code:
   If condition Then x = 1

2) Simple IF with multiple statements
Code:
   If condition Then x = 1: a = ""

3) Block IF..THEN
Code:
   If condition Then
     x = 1
     a = "xxx"
   End If

4) Block IF..THEN..ELSE
Code:
   If condition Then
     x = 1
     a = "xxx"
   Else
     x = 2
     a = "yyy"
   End If

5) Block IF..THEN..ELSEIF
Code:
   If condition1 Then
     x = 1
     a = "xxx"
   ElseIf condition2 Then
     x = 2
     a = "yyy"
   End If

6) Block IF..THEN..ELSEIF..ELSE
Code:
   If condition1 Then
     x = 1
     a = "xxx"
   ElseIf condition2 Then
     x = 2
     a = "yyy"
   ElseIf condition3 Then
     x = 3
     a = "zzz"
   Else
     x = 4
     a = "xyz"
   End If
 
Upvote 0
You might consider the SELECT statement which is rather more elegant when testing multiple conditions:-

How to code SELECT statements

1) Single test variable
Code:
   Select Case [I]variable[/I]
     Case 1, 3, 6 To 9
       x = 1
       a = "xxx"
     Case Is <= 100
       x = 2
       a = "yyy"
     Case Is <= 1000
       x = 3
       a = "zzz"
     Case Else
       x = 4
       a = "xyz"
   End Select

2) Multiple test variables
Code:
   Select Case True
     Case x < 100
       a = "xxx"
     Case a = "yyy", a = "zzz"
       x = 2
     Case Else
       x = 4
       a = "xyz"
   End Select
 
Upvote 0
Thank you Ruddles... You have always been very helpful. I will bookmark this thread and see about using these techniques where possible.
 
Upvote 0

Forum statistics

Threads
1,222,625
Messages
6,167,149
Members
452,099
Latest member
Auroraaa

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top