Reputation: 441
I am completely new to VBA. I need to write a program, which will generate an array of integer and will find an index of the minimal element. I got this error "Type mismatch: array or user-defined type expected." I looked into many similar questions, but couldn't figure out what is wrong.
Function random_integer(Min As Integer, Max As Integer)
random_integer = Int((Max - Min + 1) * Rnd + Min)
End Function
Function generate_array(Size As Integer)
Dim Arr(Size)
For i = 0 To UBound(Arr)
Arr(i) = random_integer(i - 10, i + 10)
Next
generate_array = Arr
End Function
Function find_index_of_min_elem(ByRef Arr() As Variant)
Dim Min As Integer
Min = Arr(0)
Dim MinIndex As Integer
MinIndex = 0
For i = 1 To UBound(Arr)
If Arr(i) < Min Then
Min = Arr(i)
MinIndex = i
End If
Next
find_index_of_min_elem = MinIndex
End Function
Sub task6()
A = generate_array(20)
IndexOfMinElemInA = find_index_of_min_elem(A)
MsgBox IndexOfMinElemInA
End Sub
Upvotes: 1
Views: 1011
Reputation: 1762
What about this? Make a second column of index numbers (in sequence from small to large) and then order the two rows by your original column. I've included the example below to illustrate: A = the original column of numbers, B = the column of index numbers D & E are the result of sorting A & B by column A The answer is then: the lowest number "0" was at index 7
Upvotes: 0
Reputation: 149295
The problem is the function Function find_index_of_min_elem(ByRef Arr() As Integer)
is expecting an Integer
as a parameter and you are passing a
as a Variant
a = generate_array(20)
IndexOfMinElemInA = find_index_of_min_elem(a)
The next error that you will get is on Dim Arr(Size) As Integer
. You cannot dimension an array like that.
I would recommend reading up on arrays.
There may be other errors but I have not checked those.
Upvotes: 7