Consulting

Results 1 to 13 of 13

Thread: Solved: VBA- Smallest non-negative number in a range

  1. #1

    Solved: VBA- Smallest non-negative number in a range

    Hi ... How do i write a function that determines the smallest non-negative number in a given range ?

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    =MIN(IF((A2:A20<>"")*(A2:A20>=0),A2:A20))

    which is an array formula, it should be committed with Ctrl-Shift-Enter, not just Enter.Excel will automatically enclose the formula in braces (curly brackets), do not try to do this manually.When editing the formula, it must again be array-entered.
    ____________________________________________
    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

  3. #3
    Thanks,,,I tried this, but got an error. Unfortunately, it is a discrete set of numbers and not sequential as in an array. So excel gives me an error stating that I have entered too many arguments. Any suggestions ?

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    How is the data laid out then?
    ____________________________________________
    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

  5. #5
    4 separate cells, in the same row, but not adjacent to each other

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Yeham but are they a pattern, say every 5th cell, or what?
    ____________________________________________
    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
    they skip a cell ... eg b3 d3 f3 h3... b4 d4 f4 g4 and so on.. these are prices that i want to compare and get the minimum price, weeding out '0' which doesn't make sense.. just to give you a perspective....

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Ah good, I thought there would be

    =MIN(IF((MOD(COLUMN(B3:H3)-COLUMN(B3),2)=0)*(B3:H3<>"")*(B3:H3>=0),B3:H3))

    still an array formula
    ____________________________________________
    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

  9. #9
    VBAX Regular
    Joined
    Aug 2008
    Posts
    48
    Location
    hi.. i tried =MIN(IF((MOD(COLUMN(B3:H3)-COLUMN(B3),2)=0)*(B3:H3<>"")*(B3:H3>=0),B3:H3))
    but it still returns '0' sometimes......

  10. #10
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    A picture can paint a thousand words. Why not post a sample?
    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'

  11. #11
    VBAX Regular
    Joined
    Aug 2008
    Posts
    18
    Location
    Change >=0 to just > 0, i.e.

    =MIN(IF((MOD(COLUMN(B3:H3)-COLUMN(B3),2)=0)*(B3:H3<>"")*(B3:H3>0),B3:H3))

    That way you'll only get zero if there are no positive values

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by nitzbar
    hi.. i tried =MIN(IF((MOD(COLUMN(B3:H3)-COLUMN(B3),2)=0)*(B3:H3<>"")*(B3:H3>=0),B3:H3))
    but it still returns '0' sometimes......
    That is because 0 is a non-negative number, which is what you asked for.
    ____________________________________________
    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

  13. #13
    VBAX Regular
    Joined
    Aug 2008
    Posts
    48
    Location
    my mistake.. i meant.. positive number.... i changed it accordingly... thank you..

Posting Permissions

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