Nested If statement Simple but what Am I missing?

  • Thread starter Saladsamurai
  • Start date
  • Tags
    If statement
In summary: Code=as2&camp=1789&creative=9325In summary, the author is asking why the nested if statement does not work.
  • #1
Saladsamurai
3,020
7
I am learning VBA for Excel and Am just wondering what is wrong with the logic in this statement?

Note that at the present moment, Time < 0.5 , but my program is outputting "Good Evening"


Code:
Sub Better_Nested_If()
    Dim Msg As String
    If Time < 0.5 Then Msg = "Mornin'" Else
        If Time >= 0.5 And Time < 0.75 Then Msg = "Afternoon" _
            Else: Msg = "Evening"
    MsgBox "Good " & Msg & "!"
End Sub
 
Technology news on Phys.org
  • #2
Nothing is wrong with the logic but your use of the language is incorrect. You left out the "End If" and the colon after "Else" should be left out.

Also, to avoid confusion you should put "then" and "else" statements on separate lines.

Here is an example If Then Else statement:

Code:
If X < Y then
    Msg = "X is less than Y"
ElseIf X > Y then
    Msg = "X is greater than Y"
Else
    Msg = "X is equal to Y"
End If
 
Last edited:
  • #3
Thanks :smile: BUt here's the problem with that:

You only use End If with 'block If' statements, which this is not.

Also, this is not a nested 'ElseIf' Sub.

?
 
  • #4
Saladsamurai said:
Thanks :smile: BUt here's the problem with that:

You only use End If with 'block If' statements, which this is not.

Also, this is not a nested 'ElseIf' Sub.

?

I wouldn't write the above nested like you do. It would be more readable doing how the above poster suggested. That said, maybe the "Else:" is messing up your program. If I recall correctly Colin is used to denote a label. Thus maybe the program is executing this line regardless of what happens above.

That is first perhaps it goes to your first in block and set msg=morning but then later in your program over rites it as evening. Good start if you are trying to obfuscate your code.
 
  • #5
Saladsamurai said:
Thanks :smile: BUt here's the problem with that:

You only use End If with 'block If' statements, which this is not.

Also, this is not a nested 'ElseIf' Sub.

?

Yes, I realized after I made the post that you were questioning the nested If statement. Even so I believe my example is a better solution to your problem. But if you just want to use nested IFs then you could do it this way:

Code:
If X < Y then
     Msg = "X is less than Y"
Else
     If X > Y then
          Msg = "X is greater than Y"
     Else
          Msg = "X is equal to Y"
     End If
End If
 
  • #6
Okay Thanks! I realize that these are poorly written loops, but they were part of an Intro to VBA text that I am using. I was just unclear as to why they did not work if they were published in the book.

Perhaps the Author did not test them all :/

:smile: Thanks again!
 
  • #7
I am not sure what book you are using but I used this book to learn VBA and it paid off greatly. Also, it is highly scientific and educational well beyond just learning VBA. Much better than one of VBA for Dummies text (those texts are terrible to learn from).

https://www.amazon.com/dp/0195370228/?tag=pfamazon01-20
 
Last edited by a moderator:

1. What is a nested if statement and how does it work?

A nested if statement is a conditional statement that is included within another conditional statement. It allows for multiple conditions to be evaluated and executed based on the outcome of the previous condition. It works by checking each condition in order and executing the code block associated with the first condition that evaluates to true.

2. Why would I use nested if statements in my code?

Nested if statements are useful when there are multiple conditions that need to be evaluated in a specific order. This can be used to create more complex logic in a program or to handle edge cases that wouldn't be covered by a single if statement.

3. What is the syntax for writing a nested if statement?

The syntax for a nested if statement is as follows:
if (condition1) {
     // code block if condition1 is true
     if (condition2) {
         // code block if condition2 is true
    } else {
         // code block if condition2 is false
    }
} else {
     // code block if condition1 is false
}

4. Can I have multiple levels of nested if statements?

Yes, you can have multiple levels of nested if statements. However, it's important to keep in mind that the more levels of nesting you have, the more complex and difficult to read your code may become. It's important to use nested if statements sparingly and to consider if there is a better way to structure your code.

5. Are there any alternatives to using nested if statements?

Yes, there are alternative ways to handle multiple conditions in your code, such as using switch statements or logical operators like && (and) and || (or). It's important to consider the specific needs of your program and choose the best approach for your particular situation.

Similar threads

  • Programming and Computer Science
Replies
5
Views
9K
  • Programming and Computer Science
Replies
18
Views
2K
  • STEM Academic Advising
Replies
29
Views
2K
  • STEM Academic Advising
Replies
5
Views
2K
  • Special and General Relativity
3
Replies
75
Views
3K
  • Programming and Computer Science
Replies
4
Views
11K
  • STEM Academic Advising
Replies
1
Views
926
  • STEM Academic Advising
Replies
16
Views
1K
  • Introductory Physics Homework Help
Replies
9
Views
2K
Replies
40
Views
2K
Back
Top