Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

Nested "If" statements don't always work...

I have four columns. In column BD, cells 9-16 have the following data:
"W1", "W1", "W1", "W2", "W2", "W3", "W3", "W4"
In column BE, cells 9-16 have the following data:
"NR", "R1", "R2", "R3", "R4", "R5", "R6", "R7"

You'll notice that W1 corresponds to NR, R1,& R2, W2 corresponds to R3 & R4, W3 corresponds to R5 & R6, and W4 corresponds to R7.


In column AF, cells 9-30 have the following function assigned:

=INDEX($BD9:$BD16,RANDBETWEEN(1,COUNTA($BD9:$BD16)),1)

Therefore, each of the cells in AF9:AF30 will read any of "W1", "W2", "W3", or "W4" randomly.


Here is my problem: In AV9:AV30 are supposed to analyze AF9:AF30. If, for example, the cell AF9 reads as "W1", then I want it to randomly fill AV9 with any of "NR", "R1", or "R2" (BE9:BE11).

I am currently just trying to get the formula to work, so in cell AV9 my formula currently reads as the following nested "IF" statement:

=IF(AF9="W1",INDEX($BE9:$BE11,RANDBETWEEN(1,COUNTA($BE9:$BE11)),1),+IF(AF9="W2",INDEX($BE12:$BE13,RANDBETWEEN(1,COUNTA($BE12:$BE13)),1))+IF(AF9="W3",INDEX($BE14:$BE15,RANDBETWEEN(1,COUNTA($BE14:$BE15)),1))+IF(AF9="W4","R7","NA"))))

It functions only some of the time, and only at specific times. If AV9="W1", then AV9="NR" or "R1" or "R2" randomly. But if AF9="W2" or "W3" or "W4", it reads as an error. I notice the first part of my nested IF statement refers to W1, so I can see the function only looks at the first part. What must be done to correct this?

I am only focusing on the functions of cells AF9 and AV9 right now, I will modify the final function later.
0
AlixTech
Asked:
AlixTech
  • 3
  • 3
2 Solutions
 
kmslogicCommented:
It seems like what you want is more like this (I think you have some paren problems):

Note you have to scrunch this all back together on one line I have it indented to make the paren matches easier to see:

=IF(
	AF9="W1", INDEX($BE9:$BE11, RANDBETWEEN(1,COUNTA($BE9:$BE11)),1),
		IF(AF9="W2",INDEX($BE12:$BE13,RANDBETWEEN(1,COUNTA($BE12:$BE13)),1),
			IF(AF9="W3",INDEX($BE14:$BE15,RANDBETWEEN(1,COUNTA($BE14:$BE15)),1), 
				IF(AF9="W4","R7",
					"NA"
				)
			)
		)
	)

Open in new window

0
 
redmondbCommented:
Hi, AlixTech.

The following is a simpler formula...
=IFERROR(INDEX($BE$9:$BE$16,MATCH(AF9,$BD$9:$BD$16,0)+RANDBETWEEN(0,COUNTIF($BD$9:$BD$16,AF9)-1),1),"N/A")

Edit: Changed to handle invalid input. This requires Excel 2007 or higher, so please let me know if you're using an earlier version.

Edit2: This handles earlier versions as well...
=IF(ISERROR(MATCH(AF9,$BD$9:$BD$16,0)),"N/A",(INDEX($BE$9:$BE$16,MATCH(AF9,$BD$9:$BD$16,0)+RANDBETWEEN(0,COUNTIF($BD$9:$BD$16,AF9)-1),1)))

Regards,
Brian.
0
 
AlixTechAuthor Commented:
Thanks guys, both of your formulas eliminated the "ERROR". Unfortunately, there's a new problem. "W1", "W2", "W3", and "W4" don't always correspond to ("NR", "R1", "R2"), ("R3", "R4"), ("R5", "R6"), or "R7" respectively. Neither formula acts to account for this even though they seem as if they should. Any thoughts?

I still need:

NR,R1,R2 to randomly be assigned to W1
R3,R4 to randomly be assigned to W2
R5,R6 to randomly be assigned to W3
R7 to randomly be assigned to W4
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
redmondbCommented:
AlixTech,

In all my testing the allocations were done as you say - if I've understood you correctly. Please see the attached.

Regards,
Brian.Random-Allocation.xls
0
 
AlixTechAuthor Commented:
Ah, I see. Thanks so much! I accidentally assigned the formula without appropriately modifying it for each cell in list AV9:AV30. Both formulas work perfectly, and now I can design just one formula if I wish. Thanks!!
0
 
AlixTechAuthor Commented:
Easy to understand, even for a novice like myself. Both solutions were exactly as I needed, the second was a simpler way to do it that I didn't know before, so now I know two ways to accomplish similar tasks.
0
 
redmondbCommented:
Thanks, AlixTech.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now