Bonjour,
j'essaye de coder une fonction qui renverrait le max de trois matrice, pour chacun des coefficients
par exemple si mat1 = (1 2 1) mat2 = (3 0 1) et mat3 = (0 1 4) la fonction donnerait :
function(mat1, mat2, mat3) = (3 2 4)
Alors dans mon exemple j'ai mis des matrices avec une linge pour la lisibilité mais dans la réalité j'ai n lignes et n colonnes
J'ai réussi à coder ça de cette façon :
Function matrixOfMax(mat1 As Range, mat2 As Range, mat3 As Range) As Variant
Dim tempArray() 'need a temporary variable to build the result
Dim i As Integer, j As Integer
For i = 1 To mat1.Rows.Count
For j = 1 To mat1.Columns.Count
tempArray(i, j) = Application.WorksheetFunction.Max(mat1(i, j), mat2(i, j), mat3(i, j)
Next j
Next i
matrixOfMax=tempArray
Ce code marche. Seulement maintenant, j'aimerai renvoyer un message d'erreur si un des nombres de la matrice n'est pas un entier.
J'ai écris ça :
For i = 1 To mat1.Rows.Count
For j = 1 To mat1.Columns.Count
If mat1(i, j) - Int(mat1(i, j)) = 0 And mat2(i, j) - Int(mat2(i, j)) = 0 And mat3(i, j) - Int(mat3(i, j)) = 0 Then
tempArray(i, j) = Application.WorksheetFunction.Max(mat1(i, j), mat2(i, j), mat3(i, j))
Else
MsgBox ("you must enter only integer value")
End If
Next j
Next i
matrixOfMax = tempArray
Le problème est qu'ici je vais avoir autant de fois le Msgbox qu'il y a de nombre non entiers dans mes 3 matrices
J'ai donc essayé avec deux Exit For :
For i = 1 To mat1.Rows.Count 'we are using option base 1, so starting to browse at 1
For j = 1 To mat1.Columns.Count
If mat1(i, j) - Int(mat1(i, j)) = 0 And mat2(i, j) - Int(mat2(i, j)) = 0 And mat3(i, j) - Int(mat3(i, j)) = 0 Then
tempArray(i, j) = Application.WorksheetFunction.Max(mat1(i, j), mat2(i, j), mat3(i, j))
Else
MsgBox ("you must enter only integer value")
End If
Exit For
Next j
Exit For
Next i
matrixOfMax = tempArray
Et là oui cool ça marche je n'ai qu'un seul Msgbox d'erreur ... mais la fonction principale ne marche plus et ne me renvoie dans ma matrice que le premier coefficient maximum des trois matrices en paramètres.