Consulting

Results 1 to 7 of 7

Thread: Formula Q: referencing first column number that meets specific criteria

  1. #1

    Question Formula Q: referencing first column number that meets specific criteria

    I am because it's for sure something really obvious that I am missing.
    I have a table in cells D through K with numberic values ranging from 0 to 1Billion. In cell B2 I want to return the reference to the first column where the value is greater than the value in A2.

    for example values in row 2 may be:
    D: 88,745
    E: 108,236
    F: 207,469
    G: 350,000
    etc.

    A2 is 125,000

    I want B 2 to return 6 for column F because that is the first time where the value of the cell is greater than 125K

    I know I can do multiple nested IF statements but this spreadsheet is dynamic and I know there has to be a better way.

    Thanks!
    T
    Last edited by tkaplan; 04-07-2014 at 09:43 AM. Reason: more descriptive title of thread

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

    =MATCH(A2,2:2,1)
    ____________________________________________
    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
    VBAX Regular
    Joined
    Jun 2013
    Posts
    16
    Location
    Are you looking for a worksheet method or would a macro work? It would be very easy to assign a button that looked up and returned the first reference where the value was greater.

    HTML Code:
    Sub Button1_Click()
    
    For Each Number In Range("d2", Range("d2").End(xlToRight))
        If Number.value > Range("a2").value Then Exit For
    Next
    Range("b2").value = Number.Cells.Address
    
    End Sub
    If you insert a button and double click on it, or assign a new macro, you can paste this code into it. This searches each number in the range of data starting at D2 (assuming no blank cells) and then returns the cell address where the value is greater than A2, putting the address in cell B2

  4. #4
    for some reason my response to xld didn't post yesterday. thanks xld, it worked.
    question just for my knowledge: how come if instaed of putting a reference to A2 i put an actual value (ie Match(125000,2:2,1)) i get an error?

    khu, i was looking for a formula for a spreadsheet. thank you for taking the time to respond though!

  5. #5
    VBAX Regular
    Joined
    Jun 2013
    Posts
    16
    Location
    khu, i was looking for a formula for a spreadsheet. thank you for taking the time to respond though![/QUOTE]


    Yeah, sorry I hadn't realized I kept this open without refreshing before posting. For some reason I was thinking you needed the actual address. I believe that match function posted above will be the cell before the one you're looking for though. The match type of 1 is the largest value that is less than or equal to you're look up value, so unless F2 is equal to A2, it will return the position of E2, since it's the first one less than or equal to A2. You'll have to add one to the match function, =MATCH(A2,2:2, 1)+1 to get the correct position.

    Also, not what you asked for, but just to put it out there, for the actual cell address instead of relative position in the search array, you can use =CELL("address",OFFSET(INDEX(D2:K2,1,MATCH(A2,D2:K2,1)),0,1)) to return $F$2.
    Last edited by khu; 04-08-2014 at 01:29 PM.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by tkaplan View Post
    question just for my knowledge: how come if instaed of putting a reference to A2 i put an actual value (ie Match(125000,2:2,1)) i get an error?
    Works fine for me.
    ____________________________________________
    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
    Quote Originally Posted by xld View Post
    Works fine for me.
    weird. will have to try again when i'm back in the office. was probably leaving something stupid out.

    anyway, issue is solved. so thank you everyone for all of your help

Posting Permissions

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