Consulting

Results 1 to 8 of 8

Thread: Solved: Different expressions of a simple formula

  1. #1
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location

    Solved: Different expressions of a simple formula

    I've recently come across a situation where it was asked for a formula for the following scenario:

    A1 is a boolean value (True or False)

    A2 formula required to show if A1 value is true then show 1 else 0.

    A number of alternatives were provided,

    =IF(A1=True,"1","0") which stores the value as Text
    =IF(A1=True,1,0) which stores the value as a Number

    There were three other alternatives which I hadn't realised could also be used

    =IF(A1,1,0) which stores the value as a Number. I'm told this can be used where the A1 is a boolean function.

    --A1 which stores the value as a Number, and
    N(A1) also stores the value as a Number.

    Can someone explain why the last two work?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    On an worksheet, Excel converts logical values to numbers with FALSE=0 and TRUE=1.
    (VB is different, CLng(True) = -1 : CLng(False) = 0, note the negative True.)

    Forcing a logical value to do numeric arithmatic is one way to convert logicals to numbers:

    --(TRUE) = (-1)*(-1)*(TRUE) = (-1)*(-1)*(1) = 1
    --(FALSE) = (-1)*(-1)*(FALSE) = (-1)*(-1)*(0) = 0

    another, slightly slower, conversion is =A1+0

    The spreadsheet function N converts other data types, including logicals, into numbers.

    The arithmetic equivalent of logical operators are:
    N(AND(A1,B1)) = N(A1)*N(B1)
    N(NOT(A1)) = 1-N(A1)
    N(OR(A1,B1)) = SIGN(N(A1) + N(B1))

    these are useful when constructing SUMPRODUCT's and other array formulas.

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    True on a spreadsheet is equal = 1 (enter =True*1 in a cell), False = 0.
    --A1 will force True/False into a number as will N(A1)

    Actually, it appears any number except 0, including negative equates to True in the IF function Try =IF(4-2,"True","False"), and any other values.

    It looks like IF is really saying "If Not Zero, then True, else False"
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Good catch.

    =OR(A1:A10) will test if there are non-zero numbers in that range
    =AND(A1:A10) tests if there are any 0's in a range.

    Nice!
    Thanks, mdmackillop.

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Ted,

    It is all to do with coercing a value into a number.

    The things that you are alluding to here is why you rarely if ever need IF tests in conditional formatting. SO often I have seen

    =IF(condition,TRUE,FALSE)

    beacuse the developer knew that CF needs to test TRUE or FALSE, but not realising that the condition returns TRUE or FALSE, so all that is needed is

    =condition

    shorter, sharper, and more efficient, doubly important in CF.

    The double unary is a common trick, widely used in SUMPRODUCT solutions, and I have offered many solutions where I have used a formula as simple as

    =--A1

    I also use it to setup non-ambiguous dates in formulae, Yanks take note!, such as

    =SUMPRODUCT(--(A1:A10>=--"2008-01-01"),--(A1:A10<--"2008-02-01"))

    N wouldn't work here, but even where it does, -- is often preferable because it isn't a function.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by mdmackillop
    Actually, it appears any number except 0, including negative equates to True in the IF function Try =IF(4-2,"True","False"), and any other values.
    That is also the case in VBA

    ?cbool(2)
    True
    ?cbool(-2)
    True
    ?cbool(0)
    False
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Thanks Bob.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    Good on you..... I am struggling to come to terms with Post #5 when you drop #6 on me.

    #6 could well pass for Russian co-ordinates to bomb the local chookpen.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •