What did I do wrong in this UDF to use XMatch?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,687
Office Version
  1. 365
Platform
  1. 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.

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
CDEF
622#VALUE!3
735#VALUE!3
8510#VALUE!2
9#VALUE!1
10#VALUE!2
Sheet3
Cell Formulas
RangeFormula
E6:E10E6=RndWtdIndex($D$6:$D$8)
F6:F10F6=XMATCH(RAND()*10,$D$6:$D$8,1)
D6D6=C6
D7:D8D7=D6+C7

Thanks
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
You're not passing an array of ranges, so the declaration is wrong, and you didn't qualify the XMatch call. Try:

VBA Code:
Function RndWtdIndex(pCumWts As Range) As Integer

   Dim CumWts() As Variant
   CumWts = pCumWts.Value

  RndWtdIndex = Application.XMatch(Rnd * CumWts(UBound(CumWts), 1), CumWts, 1)

End Function
 
Upvote 0
Solution
You're not passing an array of ranges, so the declaration is wrong, and you didn't qualify the XMatch call. Try:

VBA Code:
Function RndWtdIndex(pCumWts As Range) As Integer

   Dim CumWts() As Variant
   CumWts = pCumWts.Value

  RndWtdIndex = Application.XMatch(Rnd * CumWts(UBound(CumWts), 1), CumWts, 1)

End Function
Doh! 😣 Of course. I initially had it coded as RndWtdIndex(pCumWts() as Variant), then realized that it was a range and forgot to remove the ().

And my call didn't work because I forgot the "." before XMatch .XMatch. That's what you mean by "qualify", right?

Here's the working code.

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

Thank you
 
Upvote 0
Correct.

FWIW, there's not much point to a With block if you only call one method of the relevant object.
The code I posted is just a sample of what will become a much larger UDF. The With block will make more sense then. I was just practicing. I need a lot of practice. 😉😒
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,269
Members
452,628
Latest member
dd2

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