JenniferMurphy
Well-known Member
- Joined
- Jul 23, 2011
- Messages
- 2,535
- Office Version
- 365
- Platform
- Windows
I am working on a UDF to generate a random set of counting numbers on [1,N]. For example, "3 2 5 4 1".
Now I want to write another UDF to test it. What I have come up with is to split the result string into an array, create another array of the same length, loop through the result array setting a code in the second array. If I find a code already set, the code fails. Here's my code. Is there a better way?
Now I want to write another UDF to test it. What I have come up with is to split the result string into an array, create another array of the same length, loop through the result array setting a code in the second array. If I find a code already set, the code fails. Here's my code. Is there a better way?
VBA Code:
Function TestRandSelect(pM As Long, pN As Long) As Variant
Dim iTally As Long
Dim Result As Variant
Dim ResultArray As Variant
Dim TallyArray() As Variant
Dim NextRand As Long
ReDim TallyArray(1 To pN)
' To be done in a loop
Result = RandSelect1(pM, pN)
ResultArray = Split(Result)
For iTally = 1 To pM
NextRand = ResultArray(iTally)
If TallyArray(NextRand) <> "" Then
TestRandSelect = "Error"
Exit Function
End If
TallyArray(NextRand) = "X"
Next iTally
TestRandSelect = "OK"
End Function