JenniferMurphy
Well-known Member
- Joined
- Jul 23, 2011
- Messages
- 2,691
- Office Version
- 365
- Platform
- Windows
I need a UDF that can use the XMatch worksheet function to find a random element in a list of cumulative weights. Here's my code. It's in a code module for this .xlsm workbook with other UDFs that do work.
And here is how it is called from the worksheet:
Thanks
VBA Code:
Function RndWtdIndex(pCumWts() As Range) As Integer
Dim CumWts() As Variant
CumWts = pCumWts.Value
With Application
RndWtdIndex = XMatch(Rnd * CumWts(UBound(CumWts), 1), CumWts, 1)
End With
End Function
And here is how it is called from the worksheet:
Typing Tutor Adaptive Learning Algorithm.xlsm | ||||||
---|---|---|---|---|---|---|
C | D | E | F | |||
6 | 2 | 2 | #VALUE! | 3 | ||
7 | 3 | 5 | #VALUE! | 3 | ||
8 | 5 | 10 | #VALUE! | 2 | ||
9 | #VALUE! | 1 | ||||
10 | #VALUE! | 2 | ||||
Sheet3 |
Cell Formulas | ||
---|---|---|
Range | Formula | |
E6:E10 | E6 | =RndWtdIndex($D$6:$D$8) |
F6:F10 | F6 | =XMATCH(RAND()*10,$D$6:$D$8,1) |
D6 | D6 | =C6 |
D7:D8 | D7 | =D6+C7 |
Thanks