tags:

views:

176

answers:

2

In a spreadsheet I'm using, some of the possibilities for a cell is being blank, having the number zero, or having a non-zero number, or having a string.

If I want to test if the cell is zero, but is not blank, I currently do

=IF(AND(B2=0,LEN(B2)>0),foo,bar)

Is there a way of replacing the two conditionals with one conditional?

+3  A: 

You can certainly use the function isblank(B2) instead of testing for length > 0.

But since you're testing for two conditions: not blank, and 0, I don't see how you can avoid two conditionals--unless--you're happy that 0 equates to not blank (which it does), then all you have to test for is 0.

+1  A: 

The function you are looking for is the ISBLANK() function.

=IF(AND(ISBLANK(A1)=FALSE,A1=0),"foo","bar")

EDIT:
Sorry I just re-read your question. You're forced to use 2 conditionals because you're checking for two conditions. If you wanted to just use one condition for whatever reason the only way that I can think of is to extract the conditions you are checking into a method and then call it.

So, it becomes

=IF(CheckCellIsOK(A1),"foo","bar")

The method still checks both conditions but you're just hiding that fact.

Gavin Chin
What's a good website for learning how to create an excel method?
Andrew Grimm
You'll need to search for VBA tutorials as that's the programming language Excel uses. Effectively you're creating Macros/Modules.Here is a couple I just googled (VBA Tutorial):http://www.anthony-vba.kefra.com/vba/vbabasic1.htm#Creating_Your_First_Macrohttp://www.vbtutor.net/VBA/vba_tutorial.htmlThe way I learnt it was to look through the object model for Excel in the microsoft msdn and to record macros then change them to be dynamic. The macro recording is a quick way to find what you want.
Gavin Chin