Certainly! Below is a VBA macro that performs matrix multiplication on two 3x3 matrices directly from Excel cells. The macro reads the input matrices from specified cell ranges, multiplies them, and outputs the resulting matrix to another specified range.
Setup Instructions
Input Matrices:
Matrix A: Place your first 3x3 matrix in cells A1:C3.
Matrix B: Place your second 3x3 matrix in cells E1:G3.
Output Matrix:
The resulting matrix will be placed in cells I1:K3.
You can modify the cell ranges in the VBA code below if your matrices are located in different cells.
VBA Code
vba
Sub Multiply3x3Matrices()
Dim MatrixA(1 To 3, 1 To 3) As Double
Dim MatrixB(1 To 3, 1 To 3) As Double
Dim Result(1 To 3, 1 To 3) As Double
Dim i As Integer, j As Integer, k As Integer
Dim ws As Worksheet
' Set the worksheet (modify if needed)
Set ws = ThisWorkbook.ActiveSheet
' Read Matrix A from A1:C3
For i = 1 To 3
For j = 1 To 3
MatrixA(i, j) = ws.Cells(i, j).Value
Next j
Next i
' Read Matrix B from E1:G3
For i = 1 To 3
For j = 1 To 3
MatrixB(i, j) = ws.Cells(i, j + 4).Value ' E is column 5
Next j
Next i
' Perform Matrix Multiplication: Result = MatrixA * MatrixB
For i = 1 To 3
For j = 1 To 3
Result(i, j) = 0
For k = 1 To 3
Result(i, j) = Result(i, j) + MatrixA(i, k) * MatrixB(k, j)
Next k
Next j
Next i
' Output the Result to I1:K3
For i = 1 To 3
For j = 1 To 3
ws.Cells(i, j + 8).Value = Result(i, j) ' I is column 9
Next j
Next i
MsgBox "Matrix multiplication complete! Result is in cells I1:K3.", vbInformation
End Sub
How to Use the Macro
Open the VBA Editor:
Press ALT + F11 in Excel to open the VBA editor.
Insert a New Module:
In the VBA editor, go to Insert > Module.
Paste the VBA Code:
Copy the VBA code provided above and paste it into the new module.
Run the Macro:
Close the VBA editor.
Go back to Excel, press ALT + F8 to open the Macro dialog box.
Select Multiply3x3Matrices and click Run.
Customizing the Cell Ranges
If your matrices are located in different cells, you can modify the cell references in the VBA code:
Matrix A:
Change ws.Cells(i, j).Value to point to the top-left cell of your Matrix A.
Matrix B:
Currently, it's set to columns E1:G3. Modify j + 4 to align with the starting column of Matrix B.
Result Matrix:
The output is set to start at column I (9). Change j + 8 to the desired starting column for the result.
Example
Assume you have the following matrices:
Matrix A (A1:C3):
code
1 2 3
4 5 6
7 8 9
Matrix B (E1:G3):
code
9 8 7
6 5 4
3 2 1
After running the macro, the Result Matrix (I1:K3) will be:
code
30 24 18
84 69 54
138 114 90
Notes
Error Handling: This macro assumes that the specified ranges contain numeric values. Ensure that there are numbers in the input matrices to avoid runtime errors.
Dynamic Ranges: For more flexibility (e.g., different matrix sizes or dynamic ranges), additional coding would be required. This example focuses on 3x3 matrices as per your request.
Automation: You can assign this macro to a button in your worksheet for easier access.
Feel free to modify and expand this code to better fit your specific requirements!