VBA Macro Matrix Multiplication with a scalar

Closed
VBAConfused - Aug 6, 2009 at 10:54 AM
 MANMANI - Jul 8, 2011 at 08:17 AM
Hello,

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.
Related:

4 responses

venkat1926 Posts 1863 Registration date Sunday June 14, 2009 Status Contributor Last seen August 7, 2021 811
Aug 9, 2009 at 09:09 PM
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 :
In excel take it as A1,B1, A2 ,B2.
A B
2 3
4 5

Now you want to make a scalar multiplication of a matrix for e.g by 6 it is
Is it
12 18
24 30
Or is it sum of these four figures(sorry I have forgotten)

I am only loudly thinking
Why not try this macro

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"
4