Cloclo1990
New Member
- Joined
- May 21, 2019
- Messages
- 4
Dear All,
I have been trying to work on this VBA question:
I have tried to start the coding but I am stuck on finding if mat1 and mat2 have the same size or not? And choosing the highest value...
Thank you for your time ! Have a great day
Option Explicit
Option Base 1
Function MatrixOfMax(Mat1 As Range, Mat2 As Range) As Variant
'Declare variables
Dim Mat1() As Variant, Mat2() As Variant
Dim i As Integer, j As Integer
'Ask User to fill up matrix 1 and 2
MsgBox "Hello. Please make sure you filled up Matrix 1 and Matrix 2"
'Setting the size of the matrix 1 and 2 to the user input
ReDim Mat1(size, size)
ReDim Mat2(size, size)
'Setting the value of matrix 1
For i = 1 To size
For j = 1 To size
Mat1(i, j) = i + j
Next j
Next i
createMatrix = Mat1
'Setting the value of matrix 2
For i = 1 To size
For j = 1 To size
Mat1(i, j) = i + j
Next j
Next i
createMatrix = Mat2
'Verify the two matrix are of the same size
'Choose highest value of each matrix
'Show the new matrixin cells I2:M5 of excel file
End Function
I have been trying to work on this VBA question:
- You must now create a user-defined function that will take two parameters of type range (two matrices mat1 and mat2). The function returns a matrix C in which each element is equal to the max value with the same coordinates in mat1 and mat2: ??,? = ???(???1?,?, ???2?,?)
The function must be used as an array-based formula in Excel. Its exact declaration must be:
Function matrixofMax(mat1 as Range, mat2 as Range) as Variant
If mat1 and mat2 do not have the same size, an explicit error message must be returned.
I have tried to start the coding but I am stuck on finding if mat1 and mat2 have the same size or not? And choosing the highest value...
Thank you for your time ! Have a great day
Option Explicit
Option Base 1
Function MatrixOfMax(Mat1 As Range, Mat2 As Range) As Variant
'Declare variables
Dim Mat1() As Variant, Mat2() As Variant
Dim i As Integer, j As Integer
'Ask User to fill up matrix 1 and 2
MsgBox "Hello. Please make sure you filled up Matrix 1 and Matrix 2"
'Setting the size of the matrix 1 and 2 to the user input
ReDim Mat1(size, size)
ReDim Mat2(size, size)
'Setting the value of matrix 1
For i = 1 To size
For j = 1 To size
Mat1(i, j) = i + j
Next j
Next i
createMatrix = Mat1
'Setting the value of matrix 2
For i = 1 To size
For j = 1 To size
Mat1(i, j) = i + j
Next j
Next i
createMatrix = Mat2
'Verify the two matrix are of the same size
'Choose highest value of each matrix
'Show the new matrixin cells I2:M5 of excel file
End Function