Can a UDF handle a sheet range and an array from another UDF?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,532
Office Version
  1. 365
Platform
  1. Windows
This UDF selects a random element from a list of cumulative weights and returns the index. As written, it can be called from another UDF. Is there a way I can code it so that it can also be called from a sheet passing it a column of numbers? Or do I need to write a pre-UDF to accept the range and convert it to an array and then call the UDF?

The function is still in test, which is why all of the Debug statements.

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

Debug.Print ""
Debug.Print "RndWtdIndex..."
Debug.Print "pCumWts = " & pCumWts(1) & " " & pCumWts(2) & " " _
          & pCumWts(3) & " " & pCumWts(4) & " " & pCumWts(5)
'Dim CumWts() As Variant
'CumWts = pCumWts.Value

Dim r As Double
Dim needle As Double

With Application
  Debug.Print "Max CumWts = " & pCumWts(UBound(pCumWts))
  r = Rnd
  Debug.Print "Rnd = " & Format(r, "0.00000")
  needle = r * pCumWts(UBound(pCumWts))
  Debug.Print "Needle = " & Format(needle, "0.0000")
  RndWtdIndex = .XMatch(needle, pCumWts, 1) - 1  '-1 because 0 origin?
  Debug.Print "RndWtdIndex = " & RndWtdIndex
End With
Debug.Print ""

End Function
Thanks
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
You'd have to change the parameter to pCumWts As Variant then test the input type in the function.
 
Upvote 0
You'd have to change the parameter to pCumWts As Variant then test the input type in the function.
Ok. Can you help me with the tests?

Do I use the VarType function?

I see vbArray as one option, but it always has another for the type. So I guess I could do
VBA Code:
If vartype(pCumWts) = vbArray + vbDouble then ...

but I don't see a test for a range. How do I test for that?

Thanks
 
Upvote 0
I'd use TypeName for the range test - for example:

VBA Code:
   If TypeName(pCumWts) = "Range" Then
      Debug.Print "It's a range"
   ElseIf VarType(pCumWts) And vbArray = vbArray Then
      Debug.Print "It's an array of some kind"
 
Upvote 0

Forum statistics

Threads
1,215,003
Messages
6,122,655
Members
449,091
Latest member
peppernaut

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