1

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
3
  • I am completely new to VBA.... Did you write this code? Commented Jun 28, 2017 at 18:53
  • Yes. I have experience in other languages Commented Jun 28, 2017 at 18:54
  • 1
    You'll want to drop the snake_case and adopt PascalCase for member names. In VBA the underscore has a special meaning; your functions can't be refactored into an interface named like that. Commented Jun 28, 2017 at 18:58

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

15 Comments

Congrats on reaching that 100K! Have another!
I seriously think so =) - "Assuming you have a valid email address on your profile and you are opted in to receiving emails from us, you should get an automated email within a week of hitting the reputation threshold. If not, check your spam folder and make sure it didn't end up there."
News Alert! SO Mug up on eBay!
Congrats again - next stop 250K!
Late into the party! congratulations :)
|
0

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

example screenshot

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.