View Full Version : Help with IF statement


Autoeng
07-31-2008, 09:57 AM
I can't get this IF statement to return correctly.

I have a field that can contain 4 answers: MP, MP1, MP2, or blank.

If MP return "Not Assigned"
If MP1 return "User 1"
IF MP2 return "User 2"
If blank return blank



=IF(C8="MP1","User 1",IF(C8="MP2","User 2",IF(C8="MP","Not Assigned",""))


With this I am getting a blank return even when C8 contains MP1. What do I need to change?

pbaldy
07-31-2008, 10:03 AM
There's an error in that formula. This appears to work:

=IF(C8="MP1","User 1",IF(C8="MP2","User 2",IF(C8="MP","Not Assigned","")))

Autoeng
07-31-2008, 10:22 AM
I still cannot get this to work. You say you had it working?

pbaldy
07-31-2008, 10:33 AM
Yes, see attached.

Autoeng
07-31-2008, 12:30 PM
Got it! Now how can I test for more than 7 conditions (MP1, MP2, MP3, ect.)?

Brianwarnock
07-31-2008, 01:08 PM
I would write a function and use the Switch Function

Public Function fswitch(c As Range)
fswitch = Switch(c = "MP1", "User !", c = "MP2", "User 2", .............)
End Function

then
=fswitch(C8)


Brian

Autoeng
07-31-2008, 01:10 PM
I am trying a Lookup but getting 50/50 results and can't figure it out.

Brianwarnock
07-31-2008, 01:14 PM
I like the switch, but a lookup could work and might be easier to maintain.

Its getting late but how are you doing it?

Brian

Autoeng
07-31-2008, 01:19 PM
=LOOKUP(A1,{"MP ","MP1 ","MP2 ",...},{"User 1","User 2","User 3",...}).

I have to work with "MP#" format as being a 6 character field as it is imported from another source. For some reason MP3, 6, 5, 8, & 9 are all returning the last User instead of the correct one. Now I have to find out why.

Brianwarnock
07-31-2008, 01:20 PM
Would
=IF(C8="MP","Not Assigned","user "& MID(c8,3,LEN(c8)-2))

work in all cases.

Brian


Ah! just seen your post, not sure if above can be made to work.

Brianwarnock
07-31-2008, 01:28 PM
I think your syntax is wrong
=LOOKUP(A1,{"MP ","MP1 ","MP2 ",...,;"User 1","User 2","User 3",...}).

Its ; between the 2 sets of references.

brian

Autoeng
07-31-2008, 01:33 PM
Either works but ; did not solve the error.

Brianwarnock
07-31-2008, 01:41 PM
Of course its character matches, the array has to be in ascending order therefore MP2 must be after MP19 etc.
Hope this helps

Brian

Brianwarnock
08-01-2008, 05:58 AM
Hi autoeng how did it go what did you use.
Personally i would use the formula =IF(C8="MP","Not Assigned","user "& MID(c8,3,LEN(c8)-2)) if the sequence is as your example.

If I were to use a Lookup I would use Vlookup as it pairs the target and result values making additions easier, as does the switch.


Brian

Autoeng
08-04-2008, 04:37 AM
Of course its character matches, the array has to be in ascending order therefore MP2 must be after MP19 etc.
Hope this helps

Brian

That explains it! Who would have thought that? Not me!

Autoeng
08-04-2008, 05:35 AM
Works perfect BUT (isn't there always a but!) how can I get this to return blank if the cell is blank? I tried """","MP","MP10",...;"""","User1","User10"... but that didn't work.

Any ideas?

And thanks for letting me know arrays had to be in sorted order!

Brianwarnock
08-04-2008, 07:30 AM
You cannot lookup a null , however just add an If
eg
If(a1="","",Lookup etc

Brian

EDIt BTW my favourite solution to this given fixed length fiels would be

=IF(A2="","",If(A2="MP","NA","user "& RIGHT(A2,4)))

But I would still prefer Vlookup with its paired offerings to Lookup, even though I would be using an extra sheet.
=VLOOKUP(A2,vlookuparray,2)
The array starts in row2
Target Result

MP Na
MP1 User1

etc

Autoeng
08-04-2008, 08:37 AM
Thanks for the IF. Works perfectly now.