Milade8080
03-15-2014, 11:23 PM
Problem solving multiplication of two matrices with entries decimal in vba code:
Option Explicit
Public MatrixA(30, 30) As Long
Public MatrixB(30, 30) As Long
Public MatrixC(30, 30) As Long
Public RowA As Integer
Public ColA As Integer
Public RowB As Integer
Public ColB As Integer
Function ProperMaticesSizes() As Boolean
If ColA <> RowB Then ProperMaticesSizes = False Else ProperMaticesSizes = True
End Function
Sub ReadMatrixA()
Dim i As Integer
Dim j As Integer
Dim intLastRow As Integer
Dim intLastCol As Integer
Sheets("Sheet1").Activate
intLastRow = ActiveSheet.UsedRange.Rows.Count
intLastCol = ActiveSheet.UsedRange.Columns.Count
For i = 1 To intLastRow
For j = 1 To intLastCol
MatrixA(i - 1, j - 1) = Cells(i, j)
Next
Next
RowA = intLastRow
ColA = intLastCol
End Sub
Sub ReadMatrixB()
Dim i As Integer
Dim j As Integer
Dim intLastRow As Integer
Dim intLastCol As Integer
Sheets("Sheet2").Activate
intLastRow = ActiveSheet.UsedRange.Rows.Count
intLastCol = ActiveSheet.UsedRange.Columns.Count
For i = 1 To intLastRow
For j = 1 To intLastCol
MatrixB(i - 1, j - 1) = Cells(i, j)
Next
Next
RowB = intLastRow
ColB = intLastCol
End Sub
Sub MultiplyMatrices()
Call ReadMatrixA
Call ReadMatrixB
If ProperMaticesSizes = False Then
MsgBox "Wrong matrices sizes. Re-dimension them"
Exit Sub
End If
Sheets("Sheet3").Activate
Dim i As Integer
Dim j As Integer
Dim k As Integer
For i = 1 To RowA
For j = 1 To ColB
MatrixC(i - 1, j - 1) = 0
For k = 1 To ColA
MatrixC(i - 1, j - 1) = MatrixC(i - 1, j - 1) + MatrixA(i - 1, k - 1) * MatrixB(k - 1, j - 1)
Next
Cells(i, j) = MatrixC(i - 1, j - 1)
Next
Next
End Sub
Not respond to decimals
Can someone tell what the problem is:
Option Explicit
Public MatrixA(30, 30) As Long
Public MatrixB(30, 30) As Long
Public MatrixC(30, 30) As Long
Public RowA As Integer
Public ColA As Integer
Public RowB As Integer
Public ColB As Integer
Function ProperMaticesSizes() As Boolean
If ColA <> RowB Then ProperMaticesSizes = False Else ProperMaticesSizes = True
End Function
Sub ReadMatrixA()
Dim i As Integer
Dim j As Integer
Dim intLastRow As Integer
Dim intLastCol As Integer
Sheets("Sheet1").Activate
intLastRow = ActiveSheet.UsedRange.Rows.Count
intLastCol = ActiveSheet.UsedRange.Columns.Count
For i = 1 To intLastRow
For j = 1 To intLastCol
MatrixA(i - 1, j - 1) = Cells(i, j)
Next
Next
RowA = intLastRow
ColA = intLastCol
End Sub
Sub ReadMatrixB()
Dim i As Integer
Dim j As Integer
Dim intLastRow As Integer
Dim intLastCol As Integer
Sheets("Sheet2").Activate
intLastRow = ActiveSheet.UsedRange.Rows.Count
intLastCol = ActiveSheet.UsedRange.Columns.Count
For i = 1 To intLastRow
For j = 1 To intLastCol
MatrixB(i - 1, j - 1) = Cells(i, j)
Next
Next
RowB = intLastRow
ColB = intLastCol
End Sub
Sub MultiplyMatrices()
Call ReadMatrixA
Call ReadMatrixB
If ProperMaticesSizes = False Then
MsgBox "Wrong matrices sizes. Re-dimension them"
Exit Sub
End If
Sheets("Sheet3").Activate
Dim i As Integer
Dim j As Integer
Dim k As Integer
For i = 1 To RowA
For j = 1 To ColB
MatrixC(i - 1, j - 1) = 0
For k = 1 To ColA
MatrixC(i - 1, j - 1) = MatrixC(i - 1, j - 1) + MatrixA(i - 1, k - 1) * MatrixB(k - 1, j - 1)
Next
Cells(i, j) = MatrixC(i - 1, j - 1)
Next
Next
End Sub
Not respond to decimals
Can someone tell what the problem is: