JenniferMurphy
Well-known Member
- Joined
- Jul 23, 2011
- Messages
- 2,691
- Office Version
- 365
- Platform
- Windows
The table below does some probability calculations using BINOM.DIST. The function calls in each cell use named ranges X and N for the first 2 parameters (@X & @N). These ranges are:
X is defined as "='Binom'!$D$10:$D$16"
N is defined as "='Binom'!$E$9:$K$9"
I have a UDF (GetFormula) that will display the formula in any cell. I use it here in M8. But I would like to be able to display the values of the first 2 parameters (2,3), rather than "@X,@N", as I show in the literal expression in M10 for the formula in H12.
I started writing GFX (Get Formula eXperimental), another UDF that will call GetFormula, take the result apart, and replace the @X and @N with the values that will be passed to BINOM.DIST from that cell (2 & 3).
GFX does the easy part of the work, as you can see in M12. It calls GetFormula, parses the result, and then puts it back together. All that is missing is the ability to replace "@X" with "2" and "@N" with "3".
Here's my GFX code. It's quick and dirty code. I'll clean it up after I get it working.
I tried several expressions including
What am I doing wrong?
Thanks
X is defined as "='Binom'!$D$10:$D$16"
N is defined as "='Binom'!$E$9:$K$9"
I have a UDF (GetFormula) that will display the formula in any cell. I use it here in M8. But I would like to be able to display the values of the first 2 parameters (2,3), rather than "@X,@N", as I show in the literal expression in M10 for the formula in H12.
I started writing GFX (Get Formula eXperimental), another UDF that will call GetFormula, take the result apart, and replace the @X and @N with the values that will be passed to BINOM.DIST from that cell (2 & 3).
GFX does the easy part of the work, as you can see in M12. It calls GetFormula, parses the result, and then puts it back together. All that is missing is the ability to replace "@X" with "2" and "@N" with "3".
Probability (test).xlsm | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
B | C | D | E | F | G | H | I | J | K | L | M | |||
3 | 0.750 | P = Probability of a single win | ||||||||||||
4 | N = Number of games left | |||||||||||||
5 | X = Number of games needed | |||||||||||||
6 | C | D | E | F | G | H | I | J | K | |||||
7 | 7 | Mass (Single) Distribution, P(X) (Cum=FALSE) | ||||||||||||
8 | 8 | N | H12: =BINOM.DIST(@X,@N,P,FALSE) | |||||||||||
9 | 9 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | ||||||
10 | 10 | X | 0 | 100.00% | 25.00% | 6.25% | 1.56% | 0.39% | 0.10% | 0.02% | H12: =BINOM.DIST(2,3,P,FALSE) | |||
11 | 11 | 1 | 75.00% | 37.50% | 14.06% | 4.69% | 1.46% | 0.44% | ||||||
12 | 12 | 2 | 56.25% | 42.19% | 21.09% | 8.79% | 3.30% | H12: =BINOM.DIST(@X,@N,P,FALSE) | ||||||
13 | 13 | 3 | 42.19% | 42.19% | 26.37% | 13.18% | ||||||||
14 | 14 | 4 | 31.64% | 39.55% | 29.66% | |||||||||
15 | 15 | 5 | 23.73% | 35.60% | ||||||||||
16 | 16 | 6 | 17.80% | |||||||||||
17 | 17 | Sum | 100.00% | 100.00% | 100.00% | 100.00% | 100.00% | 100.00% | 100.00% | |||||
Binom |
Cell Formulas | ||
---|---|---|
Range | Formula | |
C6:K6 | C6 | =col() |
M8 | M8 | =getformula(H12) |
E10,K10:K16,J10:J15,I10:I14,H10:H13,G10:G12,F10:F11 | E10 | =BINOM.DIST(X,N,P,FALSE) |
M12 | M12 | =GFX(H12) |
B7:B17 | B7 | =ROW() |
E17:K17 | E17 | =SUM(E10:E16) |
Named Ranges | ||
---|---|---|
Name | Refers To | Cells |
N | =Binom!$E$9:$K$9 | E10 |
P | =Binom!$L$3 | E10:K10, K11:K16, J11:J15, I11:I14, H11:H13, G11:G12, F11 |
X | =Binom!$D$10:$D$16 | E10:K10 |
Here's my GFX code. It's quick and dirty code. I'll clean it up after I get it working.
VBA Code:
Option Explicit
Function GFX(addr As Range)
Dim formula
Dim pcs() As String
Dim p1, p2, p3, p4, p5
'Get the text of the formula
formula = getformula(addr)
'Split off the function name
pcs = Split(formula, "(", 2, vbTextCompare)
GFX = pcs(0) & "("
'Split the rest at the commas
formula = pcs(1)
pcs = Split(formula, ",", , vbTextCompare)
'Replace P1 & P2 with an expression that will
'return the values of @X & @N at the calling cell
p1 = pcs(0)
p2 = pcs(1)
'These are OK
p3 = pcs(2)
p4 = pcs(3)
'Reassemble the parts
GFX = GFX & p1 & "," & p2 & "," & p3 & "," & p4
End Function
I tried several expressions including
Code:
?range("N").Address
$E$9:$K$9
?range("N").Address(2,3).value
$E$9:$K$9
What am I doing wrong?
Thanks