PDA

View Full Version : [SOLVED:] Multiply two matrices



Cinema
10-11-2016, 06:22 AM
Hi,


I want to multiply two matrices. The first Matrix is on Sheet2 in Range(Cells(2,2):Cells(10, col)). The second Matrix is on Sheet1 in Range(Cells(2,2):Cells(col, 2)).
The result of this product (Matrix * Vector) should be written on Sheet2 in Column A.

The Variable col varies.
My code does not work :(




Sub Makro1()


Dim tool As Workbook
Dim sh2, sh1 As Worksheet
Dim col As Integer

Set sh1 = Sheet("Sheet1")
Set sh2 = Sheets("Sheet2")
Set tool = ThisWorkbook



col = tool.sh1.Cells(tool.sh1.Rows.Count, 6).End(xlUp).Row
sh2.Activate
Range("A1:A10") = WorksheetFunction.MMult(sh2.Range("Cells(2,2):Cells(10, col)"), sh1.Range("Cells(2,2):Cells(col, 2)"))




End Sub

snb
10-11-2016, 06:49 AM
Your sample file neither.

mana
10-11-2016, 07:05 AM
Option Explicit

Sub test()
Dim tool As Workbook
Dim sh1 As Worksheet, sh2 As Worksheet
Dim col As Integer
Dim rng1 As Range, rng2 As Range

Set tool = ThisWorkbook
Set sh1 = tool.Sheets("Sheet1")
Set sh2 = tool.Sheets("Sheet2")


col = sh1.Cells(sh1.Rows.Count, 6).End(xlUp).Row

Set rng2 = sh2.Range(sh2.Cells(2, 2), sh2.Cells(10, col))
Set rng1 = sh1.Range(sh1.Cells(2, 2), sh1.Cells(col, 2))

sh2.Range("A1:A9").Value = WorksheetFunction.MMult(rng2, rng1)
sh2.Activate

End Sub

Cinema
10-11-2016, 07:18 AM
Hi mana,

i will try this code and let you know about the result. Thank you :)

Cinema
10-12-2016, 09:31 AM
Hi mana thank you your code works perfect.
I have also a little bit complicated version, but it works too:



tool.sh2.Range("A1:A9") = Application.MMult(tool.sh2.Range(tool.sh2.Cells(2, 3), tool.sh2.Cells(11, 6)), tool.sh1.Range(tool.sh1.Cells(8, 7), tool.sh1.Cells(11, 7)))

Cinema
10-12-2016, 09:33 AM
tool.sh2.Range("A1:A9") = Application.MMult(tool.sh2.Range(tool.sh2.Cells(2, 3), tool.sh2.Cells(11, 6)), tool.sh1.Range(tool.sh1.Cells(8, 7), tool.sh1.Cells(11, 7)