I am using a function to randomize the items in an array,
Dim dxr() As Variant 'declared globally
dim number as integer
public sub proc1()
ReDim dxr(number)
If number > 0 Then dxr = RandomNumbers(number, 1, number) ' ERROR occurs here on this line
end sub
I get a Runtime error 13- type mismatch randomly(sometimes it works without errors), and when I click debug and then "continue" in the popup window, the program runs again. I am calling the same procedure in >10000 iterations.
Does anybody have an idea about this? I have pasted the RandomNumbers function below.
Thanks.
Public Function RandomNumbers(Upper As Integer, _
Optional Lower As Integer = 1, _
Optional HowMany As Integer = 1, _
Optional Unique As Boolean = True) As Variant
'*******************************************************
'This Function generates random array of
'Numbers between Lower & Upper
'In Addition parameters can include whether
'UNIQUE values are required
'Note the Result is INCLUSIVE of the Range
'Debug Example:
'x = RandomNumbers(49, 1, 7)
'For n = LBound(x) To UBound(x): Debug.Print x;: Next n
'WARNING HowMany MUST be greater than (Higher - Lower)
'******************************************************
On Error GoTo LocalError
If HowMany > ((Upper + 1) - (Lower - 1)) Then Exit Function
Dim x As Integer
Dim n As Integer
Dim arrNums() As Variant
Dim colNumbers As New Collection
ReDim arrNums(HowMany - 1)
With colNumbers
'First populate the collection
For x = Lower To Upper
.Add x
Next x
For x = 0 To HowMany - 1
n = RandomNumber(0, colNumbers.Count + 1)
arrNums(x) = colNumbers
If Unique Then
colNumbers.Remove n
End If
Next x
End With
Set colNumbers = Nothing
RandomNumbers = arrNums
Exit Function
LocalError:
'Justin (just in case)
RandomNumbers = ""
End Function
Dim dxr() As Variant 'declared globally
dim number as integer
public sub proc1()
ReDim dxr(number)
If number > 0 Then dxr = RandomNumbers(number, 1, number) ' ERROR occurs here on this line
end sub
I get a Runtime error 13- type mismatch randomly(sometimes it works without errors), and when I click debug and then "continue" in the popup window, the program runs again. I am calling the same procedure in >10000 iterations.
Does anybody have an idea about this? I have pasted the RandomNumbers function below.
Thanks.
Public Function RandomNumbers(Upper As Integer, _
Optional Lower As Integer = 1, _
Optional HowMany As Integer = 1, _
Optional Unique As Boolean = True) As Variant
'*******************************************************
'This Function generates random array of
'Numbers between Lower & Upper
'In Addition parameters can include whether
'UNIQUE values are required
'Note the Result is INCLUSIVE of the Range
'Debug Example:
'x = RandomNumbers(49, 1, 7)
'For n = LBound(x) To UBound(x): Debug.Print x;: Next n
'WARNING HowMany MUST be greater than (Higher - Lower)
'******************************************************
On Error GoTo LocalError
If HowMany > ((Upper + 1) - (Lower - 1)) Then Exit Function
Dim x As Integer
Dim n As Integer
Dim arrNums() As Variant
Dim colNumbers As New Collection
ReDim arrNums(HowMany - 1)
With colNumbers
'First populate the collection
For x = Lower To Upper
.Add x
Next x
For x = 0 To HowMany - 1
n = RandomNumber(0, colNumbers.Count + 1)
arrNums(x) = colNumbers
If Unique Then
colNumbers.Remove n
End If
Next x
End With
Set colNumbers = Nothing
RandomNumbers = arrNums
Exit Function
LocalError:
'Justin (just in case)
RandomNumbers = ""
End Function