Long nested IF statements not going to subsequent steps

snipescc

Board Regular
Joined
Aug 29, 2010
Messages
130
I'm doing a large set of nested IF/AND statements that isn't working correctly. It's supposed to check if both a user ID is the same as one in the cells 10 rows away, and if the sum of 10 cells in a column is equal to 10. If it is, it subtracts some timestamps. If it isn't, it's supposed to check the same things, but for 11 rows, then 12, 13,14 ect. I've gotten the formula to work with just 2 steps (checking 10 and 11 rows) as a test, but the large 10 step one doesn't work right. If the first condition is met, it's fine, but if it isn't, sometimes (but not always) it jumps to the final "false" result. It's probably a syntax error, but I've never done an IF statement this long. Anyone spot the mistake?

Column A is a user ID, B is a timestamp, and K gives me 1 if it is the first instance of an address, and 0 if it isn't. The idea is to see how long it takes for someone to go to 10 addresses.


The one that works:
=IF(AND(A39=A30,SUM(K30:K39)=10),B39-B30,IF(AND(A39=A29,SUM(K29:K39)=10),B39-B29,"unmeasured"))

The one that doesn't:
=IF(AND(A25=A16,SUM(K16:K25)=10),B25-B16,IF(AND(A25=A15,SUM(K15:K25)=10),B25-B15,IF(AND(A25-A14,SUM(K14:K25)=10),B25-B14,IF(AND(A25=A13,SUM(K13:K25)=10),B25-B13,IF(AND(A25=A12,SUM(K12:K25)=10),B25-B12,IF(AND(A25=A11,SUM(K11:K25)=10),B25-B11,IF(AND(A25=A10,SUM(K10:K25)=10),B25-B10,IF(AND(A25=A9,SUM(K9:K25)=10),B25-B9,IF(AND(A25=A8,SUM(K8:K25)=10),B25-B8,IF(AND(A25=A7,SUM(K7:K25)=10),B25-B7,"unmeasured"))))))))))

The same formula with line breaks for easy reading:
IF(AND(A25=A16,SUM(K16:K25)=10),B25-B16,
IF(AND(A25=A15,SUM(K15:K25)=10),B25-B15,
IF(AND(A25-A14,SUM(K14:K25)=10),B25-B14,
IF(AND(A25=A13,SUM(K13:K25)=10),B25-B13,
IF(AND(A25=A12,SUM(K12:K25)=10),B25-B12,
IF(AND(A25=A11,SUM(K11:K25)=10),B25-B11,
IF(AND(A25=A10,SUM(K10:K25)=10),B25-B10,
IF(AND(A25=A9,SUM(K9:K25)=10),B25-B9,
IF(AND(A25=A8,SUM(K8:K25)=10),B25-B8,
IF(AND(A25=A7,SUM(K7:K25)=10),B25-B7,"unmeasured"))))))))))
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Ah! problem solved. One of the equal signs was a minus sign. 422 character formula, thrown off by a slip of the finger.
 
Upvote 0
If you are interested in an alternative to the long nested IF statements, you could try the formulas shown below.

This is more scalable and allows you to find the elapsed time for trips to a variable number of addresses entered in B28.


Excel 2013
ABCDEFGHIJK
6UserIDTimestampFirst Instance
7ID_1017:000
8ID_1017:300
9ID_1018:000
10ID_1018:301
11ID_1029:000
12ID_1029:301
13ID_10210:000
14ID_10210:300
15ID_10211:001
16ID_10211:301
17ID_10212:000
18ID_10212:301
19ID_10213:001
20ID_10213:301
21ID_10214:001
22ID_10214:301
23ID_10215:000
24ID_10215:301
25ID_10216:001
26
27
28Address Count10
29Index of 1st Trip6
30Elapsed Time6:30
Sheet1
Cell Formulas
RangeFormula
B30=IFERROR(IF($A$25=INDEX($A$7:$A$16,$B$29),$B$25-INDEX($B$7:$B$16,$B$29),NA()),"unmeasured")
B29{=IF(SUM($K$7:$K$25)<$B$28,NA(),MATCH(SUM($K$7:$K$25)-$B$28+1,SUBTOTAL(9,INDIRECT("$K$7:"&ADDRESS(ROW($K$7:$K$25),COLUMN($K$7)))),0))}
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
If using Excel 2010 or later, & with Jerry's layout & idea of putting the number of addresses required in B28, you could also try this

Excel Workbook
ABCDEFGHIJK
6UserIDTimestampFirst Instance
7ID_1017:000
8ID_1017:300
9ID_1018:000
10ID_1018:301
11ID_1029:000
12ID_1029:301
13ID_10210:000
14ID_10210:300
15ID_10211:001
16ID_10211:301
17ID_10212:000
18ID_10212:301
19ID_10213:001
20ID_10213:301
21ID_10214:001
22ID_10214:301
23ID_10215:000
24ID_10215:301
25ID_10216:001
26
27
2810
29
306:30
Elapsed Time
 
Upvote 0
Ah... very nice Peter. I hadn't noticed the Aggregate function previously. Thanks for pointing that out. :)
No problem. I'm gradually getting a bit used to Aggregate but find it frustrating that you can't feed array-like data into it for many of its function numbers. :(
 
Upvote 0

Forum statistics

Threads
1,216,416
Messages
6,130,486
Members
449,584
Latest member
LeChuck

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top