VBA Macro Matrix Multiplication with a scalar
Last answer on Aug 16, 2009 9:28:30 pm BST VBAConfused, on Aug 6, 2009 3:54:37 pm BSTHello,
I've been struggling with what should be a simple operation using Excel VBA and was hoping someone could please help me out. Basically I have 2 ranges declared as:
Dim matrixR As Range
Dim matrixW As Range
and initiated as:
Set matrixR = Range(Range("A9").End(xlDown), Range("A9").End(xlToRight))
Set matrixW = Range(Range("A9").End(xlDown).Offset(4, 0), Range("A9").End(xlToRight).End(xlDown).Offset(3 + NumConcepts, 0))
The size of the ranges depends on how many concepts the user inputs (i.e. 5 nodes = 5X5 matrix).
MatrixR is populated whereas matrixW is the one I want to fill. I'm trying to multiply every value in matrixR with a constant and set it equal to matrixW.
matrixW.Value = matrixR.value * 0.9
gives me an error. I have also tried various loops and defining a FormulaArray to no avail.
I'd really appreciate any help.
Configuration: Windows Vista Firefox 3.0.13
It would be helpful if member give the vesion of office they are hving
|
Thanks for the reply, sorry I forgot to mention I am using the office 2007 version of excel. The link you have provided is useful. However, it does not really apply to my problem as I need to implement multiplication of a matrix with a scalar using VB.
|
Even though I have done matric multiplication manually during my young days as a student of Mathematics it is long ago I have forgotten, If I remember correctly if you have matrix(trivial example) as below :
Sub test()
Dim rng As Range, c As Range
Dim scalar As Double
scalar = 0.9
Set rng = Range(Range("a1"), Range("a1").End(xlDown).End(xlToRight))
For Each c In rng
c.Value = c * scalar
Next c
End Sub
If you want total you ca use this macro Sub test1()
Dim rng As Range, c As Range
Dim scalar As Double, total As Double
scalar = 0.9
Set rng = Range(Range("a1"), Range("a1").End(xlDown).End(xlToRight))
For Each c In rng
c.Value = c * scalar
Next c
total = WorksheetFunction.Sum(rng)
MsgBox total
End Sub
Will this of any use? Probably not! if it is multiplication of matrix over matrix there is a function in excel 2007 caled MMULT This formula is to be invoked by control+shift+enter see help under "matrix" |

