# Office 365 >  > [SOLVED] Excel Userform cannot perform multiple Ifs and Elses.

## KSChan

I hope someone will solve these problems for me. I'm using Excel in Office 365.

In the userform I have some codes and a few of them need some conditions to display the results.
However, when I used multiple Ifs and Else in the coding, something went wrong. It seems that the code will run according to which comes first.

It is very difficult to explain here so I've attached 2 files: Sample1 and Sample2.

Sample1 is the problematic one. I kept getting wrong message that does not 'match' the condition.
Sample2 has all the problematic codes "removed" (use apostrophes) to inactive them.
It runs smoothly, but I've no control over what should appear in the fields that have certain conditions.

Please run the 2 files and you can see what I meant.

Thank you very much in advance.

----------


## 6StringJazzer

> Sample1 is the problematic one. I kept getting wrong message that does not 'match' the condition.



There is nothing intrinsically wrong with your code. Your problem seems to be that it does not do what you expect. However, I don't know what you expect.

Please describe the scenario where you think it fails, and what you want to happen instead under those conditions.

----------


## 6StringJazzer

I do see a problem in your logic.

When you combine And and Or in a single condition, the And takes precedence. This line of code



```
Please Login or Register  to view this content.
```


means this:



```
Please Login or Register  to view this content.
```


So if your value is under 100, this If will always be TRUE, regardless of whether the units are feet or cm.

You need to put parentheses around the Or to force it to be evaluated like this:



```
Please Login or Register  to view this content.
```


Similarly this should be



```
Please Login or Register  to view this content.
```


The same problem occurs in the second block of code that you commented out.

----------


## LeoTaxi

here the control happen by exit of textbox


Kind regards
Leo

----------


## KSChan

Thanks to 6StringJazzer for solving the If & Else problems for the height and weight. It is working as I expected. Thank you. I appreciate your help.

Thanks to LeoTaxi also for showing me another way to approach the If & Else problems.

But I encounter another problem now which I hope LeoTaxi can help: the warning message for me to fill in the name field when I want to close the userform. My original was to warn that the name is not keyed in when trying to save the data.
But now that you have made the message appear once the name field is ignored/blank, the userform will close only if I keyed in all the fields. On opening the userform and if I decided not to use it at all, say I want to view the data first, I couldn't close the userform because it keeps asking me to fill in the name field. I've to end task from file manager if I want to.
Is there a way to solve it? Thanks.

----------


## KSChan

I must apolozise to Leo for mentioning that he made the message appear once the name field is ignored or is blank.
It was I who is trying to have the message appear once the name field is ignored/blank. So I used this:



```
Please Login or Register  to view this content.
```


So as I mentioned earlier, it wouldn't allow me to close the userform if I do not want to use the form. I've to complete every fields and save before I could close it.
Any way I can solve this? Thanks.

----------


## LeoTaxi

Something like this for exit Reg2




```
Please Login or Register  to view this content.
```


Kind regards
Leo

----------


## 6StringJazzer

Moderator's note: Please take the time to *review our rules*. There aren't many, and they are all important. Rule #3 requires code tags. I have added them for you this time in post #6 to keep the thread moving, but please add them yourself next time. --6StringJazzer

----------


## KSChan

Thanks 6StringJazzer, for the reminder to use code tags.
Thanks Leo, for your new coding. It works now. The userform can close now without me having to type in all the fields.
Chan

----------


## teylyn

KSChan, why are you posting this in the Office 365 Forum? Your profile states that you use Excel 2007, so you are clearly not on 365.

If you use Office 365, please update your profile.

If you don't use Office 365, or your question is not related to Office 365 specific problems, please post in the appropriate forum and don't clutter the Office 365 forum.

----------

