help with excel macro programming

Shankar

Disciple
hi guys i got this program i am been doing. its a fairly simply program and i am fairly useless in programming. I am beginning to learn program rite now and i am doing this problem

Qn) Develop a general code for multiplying an [m x n] matrix with any [n x k] matrix is

given below. Implement the code in EXCEL VB. Verify the results using hand

calculations.

MATRIX A

1 2 3

3 4 4

2 0 6

MATRIX B

1 2

6 2

4 1

This is what i have done

Code:
Private Sub mycode_Click()

Dim i As Integer, j As Integer, n As Integer

Dim A() As Single, B() As Single, C() As Single

n = Cells(1, 2)

ReDim A(n, n) As Single, B(n, n) As Single, C(n, n) As Single

For i = 1 To n

    For j = 1 To n

        A(i, j) = Cells(1 + i, j)

    Next j

Next i

For i = 1 To n

    For j = 1 To n

        A(i, j) = Cells(2 + i, j)

    Next j

Next i

For i = 1 To n

    C(i) = 0#

    For j = 1 To n

        C(i) = C(i) + A(i, j) * B(j)

    Next j

Next i

For i = 1 To n

Cells(2 + i, n + 4) = C(i)

Next i

End Sub

can u guys see through my code and point out where i have gone wrong.

Thanks in advance
 
you need to make the calculations within the loops

here is some c code you could adopt

void mult_matrices(int a[][3], int b[][3], int result[][3])

{

int i, j, k;

for(i=0; i<3; i++)

{

for(j=0; j<3; j++)

{

for(k=0; k<3; k++)

{

result[j] = a[k] + b[k][j];

}

}

}

}
 
i want to declare a [m x n] matrix and any [n x k] matrix and be able to multiply them in vb in excel. i dunno how to get values of both the matrix from the excel sheet.

i could use help. is anybody there
 
1. When you read the matrices, you read A from Cells(1 + i, j) and B from Cells(2 + i, j). Are you sure you are reading them properly? It should be A from Cells(sra + i - 1,j) where sra is the row number where matrix A starts in Excel. Same for B.

2. You need to have 3 loops for actual computation of the matrices, so it should have been -
Code:
...
For i = 1 To n
    For j = 1 To n
       C(i,j) = 0#
       For k = 1 To n
          C(i,j) = C(i,j) + A(i,k) * B(k,j)
       Next k
    Next j
Next i
...

Hope this helps.
 
Back
Top