BetaOp9
BetaOp9

Reputation: 71

Excel VBA - Array Formula Ignoring If Statement

I'm really new to array formulas. The flow should be IF the list has NO data entered in the array or only 1 entry, it shows "Add Weight" [7 total spots). Once two pieces of any data has been input anywhere in the array, it checks for the FIRST value in the array and subtracts the LAST value in the array.

This issue I'm getting is the cell shows #value when the array is empty instead of showing "Add Weight". The formula works perfectly once data is input and correctly subtracts the last entry from first entry regardless of their placement within the array.

This looks up the last entry in the array.

LOOKUP(2,1/(1-ISBLANK(D13:D19)),D13:D19))

This looks up the first entry in the array.

INDEX(D13:D19,(MATCH(FALSE,ISBLANK(D13:D19),0)))

Is it because the formula is marked as an array?

=IF(COUNTIF(DailyProgress[Change],"")>6,"Add Weight",LOOKUP(2,1/(1-ISBLANK(D13:D19)),D13:D19))-INDEX(D13:D19,(MATCH(FALSE,ISBLANK(D13:D19),0)))

If there is a better way to write this, I'm all ears.

EDIT:

In an adjacent column I have it showing the change from the previous cell like so:

=IF([@[Weight (lbs)]]="","",[@[Weight (lbs)]]-D17) 

Where D17 is the cell above the current row [This would be entered on row 18].

As it is, if the cell right above the adjacent cell is empty it shows it as zero and does "0 - value= -value" so with the change we just did I want it to compare it to the LAST input cell before it instead of the cell directly above it. I know this will need to be rewritten completely. Still using the D13:D19 array.

Upvotes: 0

Views: 144

Answers (1)

Scott Craner
Scott Craner

Reputation: 152450

Use this array formula:

=IFERROR(IF(MATCH(FALSE,D13:D19="",0)=MATCH(1E+99,D13:D19),"Add Weight",INDEX(D13:D19,MATCH(1E+99,D13:D19))-INDEX(D13:D19,MATCH(FALSE,D13:D19="",0))),"Add Weight")

Until there are at least two entries in D13:D19 it will Return Add Weight

Being an array formula it needs to be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode. If done correctly then Excel will put {} around the formula.


FWIW:

The reason your COUNTIF did not work is the criteria, Change the "" to "=" and it would count the blanks:

COUNTIF(DailyProgress[Change],"=")>6

Upvotes: 1

Related Questions