A
Ardy
Hello All:
I needed a VB function that would rank a list of students that sign up
in our robotics after school program. Found one in the net. I am
trying to modify it to be able to user input two of the variables but
am getting errors no Mather what I do. Can somebody take look at this
and guide me of what it is I am doing wrong……
The approach the individual who wrote this is in two parts he wrote a
function that it does the randomizing of numbers and then he wrote a
VB code that fills in the variables needed and uses the function to do
it. My goal is to modify the VB code that calls the function and pass
the variables…..
My goal is to modify varrRandomNumberList = UniqueRandomNumbers(9, 1,
9) to varrRandomNumberList = UniqueRandomNumbers(AnalNum, 1,
CulmNum). I am using the InputBox to aquire the number from the user
and assign it to variables AnalNum and CulmNum. It is not working
well and keeps giving me Compile error “ByRef argument type mismatch”
------------------------------------------------------
Function----------------------------------------------------------------------------
Function UniqueRandomNumbers(NumCount As Long, LLimit As Long, ULimit
As Long) As Variant
' creates an array with NumCount unique long random numbers
' in the range LLimit - ULimit (including)
Dim RandColl As Collection, i As Long, varTemp() As Long
UniqueRandomNumbers = False
If NumCount < 1 Then Exit Function
If LLimit > ULimit Then Exit Function
If NumCount > (ULimit - LLimit + 1) Then Exit Function
Set RandColl = New Collection
Randomize
Do
On Error Resume Next
i = CLng(Rnd * (ULimit - LLimit) + LLimit)
RandColl.Add i, CStr(i)
On Error GoTo 0
Loop Until RandColl.Count = NumCount
ReDim varTemp(1 To NumCount)
For i = 1 To NumCount
varTemp(i) = RandColl(i)
Next i
Set RandColl = Nothing
UniqueRandomNumbers = varTemp
Erase varTemp
End Function
----------------------------------------------------------------------
END FUNCTION--------------------------------------------
The code that calls the function
--------------------------------------------------------VB
CODE---------------------------------------------------------------------
' example use:
Sub DoUniqueRandomNumbers()
Dim varrRandomNumberList As Variant, AnalNum As Variant, CulmNum As
Variant, X As Byte
'
' AnalNum = InputBox(prompt:="Enter Total Number of Students. ")
' If AnalNum = "" Then Exit Sub
' CulmNum = AnalNum
' MsgBox ("The Number Is " & AnalNum & "...." & CulmNum)
' MakeColumn X
'
varrRandomNumberList = UniqueRandomNumbers(9, 1, 9)
Range(Cells(2, 1), Cells(10 + 0, 1)).Value = _
Application.Transpose(varrRandomNumberList)
End Sub
--------------------------------------------------------------END VB
CODE--------------------------------------------------------
I needed a VB function that would rank a list of students that sign up
in our robotics after school program. Found one in the net. I am
trying to modify it to be able to user input two of the variables but
am getting errors no Mather what I do. Can somebody take look at this
and guide me of what it is I am doing wrong……
The approach the individual who wrote this is in two parts he wrote a
function that it does the randomizing of numbers and then he wrote a
VB code that fills in the variables needed and uses the function to do
it. My goal is to modify the VB code that calls the function and pass
the variables…..
My goal is to modify varrRandomNumberList = UniqueRandomNumbers(9, 1,
9) to varrRandomNumberList = UniqueRandomNumbers(AnalNum, 1,
CulmNum). I am using the InputBox to aquire the number from the user
and assign it to variables AnalNum and CulmNum. It is not working
well and keeps giving me Compile error “ByRef argument type mismatch”
------------------------------------------------------
Function----------------------------------------------------------------------------
Function UniqueRandomNumbers(NumCount As Long, LLimit As Long, ULimit
As Long) As Variant
' creates an array with NumCount unique long random numbers
' in the range LLimit - ULimit (including)
Dim RandColl As Collection, i As Long, varTemp() As Long
UniqueRandomNumbers = False
If NumCount < 1 Then Exit Function
If LLimit > ULimit Then Exit Function
If NumCount > (ULimit - LLimit + 1) Then Exit Function
Set RandColl = New Collection
Randomize
Do
On Error Resume Next
i = CLng(Rnd * (ULimit - LLimit) + LLimit)
RandColl.Add i, CStr(i)
On Error GoTo 0
Loop Until RandColl.Count = NumCount
ReDim varTemp(1 To NumCount)
For i = 1 To NumCount
varTemp(i) = RandColl(i)
Next i
Set RandColl = Nothing
UniqueRandomNumbers = varTemp
Erase varTemp
End Function
----------------------------------------------------------------------
END FUNCTION--------------------------------------------
The code that calls the function
--------------------------------------------------------VB
CODE---------------------------------------------------------------------
' example use:
Sub DoUniqueRandomNumbers()
Dim varrRandomNumberList As Variant, AnalNum As Variant, CulmNum As
Variant, X As Byte
'
' AnalNum = InputBox(prompt:="Enter Total Number of Students. ")
' If AnalNum = "" Then Exit Sub
' CulmNum = AnalNum
' MsgBox ("The Number Is " & AnalNum & "...." & CulmNum)
' MakeColumn X
'
varrRandomNumberList = UniqueRandomNumbers(9, 1, 9)
Range(Cells(2, 1), Cells(10 + 0, 1)).Value = _
Application.Transpose(varrRandomNumberList)
End Sub
--------------------------------------------------------------END VB
CODE--------------------------------------------------------