# Thread: Writing a macro for arrays

1. ## Writing a macro for arrays

hey guys

can someone give me a bit of advise on how to complete the below please

"Write a macro in visual basic which:
- Inputs the numbers for and populates a 2x2 array using a nested for next loop
- Displays the matrix on screen with seperate line for row 2

I have a macro that multiplys to arrays but i dont think its the right one.

Sub multiply_arrays()

Dim array1(1 To 2, 1 To 2), array2(1 To 2, 1 To 4), result

'0 1
'1 0

array1(1, 1) = 0
array1(1, 2) = 1
array1(2, 1) = 1
array1(2, 2) = 0

' 1 1 3 1
' 1 4 1 1

array2(1, 1) = 1
array2(1, 2) = 1
array2(1, 3) = 3
array2(1, 4) = 1
array2(2, 1) = 1
array2(2, 2) = 4
array2(2, 3) = 1
array2(2, 4) = 1

result = WorksheetFunction.MMult(array1, array2)

Range("A12").Select
Selection.Value = result

End Sub

2. ## Re: Writing a macro for arrays

Hey seamy20.

Are you just getting the entries for your matrix directly from a spreadsheet? If so, what is the routine to get the contents of a particular cell?

3. ## Re: Writing a macro for arrays

Im not to sure i just mannualy typed these numbers into the macro so it automatically displays them on thespreadsheet but i am a bit confused by the question, got really stuck at this question :-(

4. ## Re: Writing a macro for arrays

A for loop in VB looks something like:

Dim MyArray(1 To 10, 1 To 10) As Double

For i = 1 to 10
For j = 1 to 10