Date: Fri, 21 Sep 2007 17:43:17 -0500 (CDT) From: Mark Jacobson To: 810-022-01-fall@uni.edu Subject: Excel VBA dice rolling function... Hi 022 students, Here is the Excel VBA function that had a bug in it this afternoon in the hands-on class. --------------------------------------------------------------------------- Function theResult(d1 As Object, d2 As Object) As String If d1.Value + d2.Value = 7 Or d1.Value + d2.Value = 11 Then theResult = "Rolled 7 or 11!" ElseIf d1.Value = d2.Value Then theResult = "Doubles!" Else theResult = Str(d1.Value + d2.Value) End If End Function --------------------------------------------------------------------------- Here is some sample Excel output: A B C Die one Die two Doubles 4 3 Rolled 7 or 11! 2 2 Doubles! 3 4 Rolled 7 or 11! 4 4 Doubles! 5 1 6 4 6 10 3 4 Rolled 7 or 11! 4 4 Doubles! 2 3 5 4 1 5 3 5 8 5 3 8 6 5 Rolled 7 or 11! 3 4 Rolled 7 or 11! 2 5 Rolled 7 or 11! 2 1 3 1 2 3 Here is what the Formulas look like: A B C ------- ------- ----------------- Die one Die two 7, 11 or doubles? ------- ------- ----------------- 4 3 =theResult(A2,B2) 2 2 =theResult(A3,B3) 3 4 =theResult(A4,B4) 4 4 =theResult(A5,B5) 5 1 =theResult(A6,B6) See you on Monday. Mark