Consulting

Results 1 to 7 of 7

Thread: Solved: IF statement

  1. #1
    VBAX Tutor
    Joined
    Sep 2008
    Posts
    213
    Location

    Solved: IF statement

    I have a column of price data for a stock which due to some errors in the data has some rows which return zero. This affects my calculations so in the next column I am using the following If statement to just refer to the price above
    [VBA]=IF(B50<=1,B49,B50)[/VBA]

    This works fine for the most part, however occasionally there are 2 or more zeros in a row. So I need to fix the if statement to keep going up the list till it finds a row with a price that isnt zero in it.

    Is there a simple way to adjust this IF statement to do this?
    Thanks.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    This array formula should do it

    =IF(B50<1,INDEX(B:B,MAX(IF($B$1:B49<>0,ROW($B$1:B49)))),B50)
    ____________________________________________
    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
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    This array formula should do it

    =IF(B50<1,INDEX(B:B,MAX(IF($B$1:B49<>0,ROW($B$1:B49)))),B50)
    ____________________________________________
    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

  4. #4
    VBAX Tutor
    Joined
    Sep 2008
    Posts
    213
    Location
    Xld, thanks. I just tried it. The cells with correct numbers work ok, but the cells that have 0 in them just return #VALUE! Am I missing something here?

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Did you array enter it?
    ____________________________________________
    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
    VBAX Tutor
    Joined
    Sep 2008
    Posts
    213
    Location
    Ok, didnt do that, ti works now. I have a few hundred rows though. I need this to be a multi-cell array formula right? (just going off the excel help here)

    Thanks

  7. #7
    VBAX Tutor
    Joined
    Sep 2008
    Posts
    213
    Location
    Actually its ok, just realized after I add the first one, I can just copy and drag it down the whole column.

    Thanks a lot for 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
  •