L
LesHurley
About a week ago I posted a question about using VBA functions in formulas.
Here
it is again with a much simpler function. XtoN raises a square matrix to an
integer power. The code is as follows. Notice that the third line is active
while the fourth is commented. As it stands here the call =xton(xton(M,2),2)
works ok but an essential test is missing. If I comment the third line and
make the fourth line active, the same call makes the function crash. Can
anyone say why, and how can I make the same test without crashing?
Function XtoN(Matrix, N)
Fraction = Abs(N) - Int(Abs(N))
'(Line 3)
If Fraction <> 0 Then
'(Line 4)
'If Matrix.Rows.Count <> Matrix.Columns.Count Or Fraction <> 0 Then
MsgBox "X must be a Square Matrix" & vbNewLine & "N must be an
Integer => 1"
Exit Function
End If
Dim i As Integer
B = Matrix
For i = 1 To N - 1
B = WorksheetFunction.MMult(Matrix, B)
Next i
XtoN = B
End Function
'Thanks for your help
Here
it is again with a much simpler function. XtoN raises a square matrix to an
integer power. The code is as follows. Notice that the third line is active
while the fourth is commented. As it stands here the call =xton(xton(M,2),2)
works ok but an essential test is missing. If I comment the third line and
make the fourth line active, the same call makes the function crash. Can
anyone say why, and how can I make the same test without crashing?
Function XtoN(Matrix, N)
Fraction = Abs(N) - Int(Abs(N))
'(Line 3)
If Fraction <> 0 Then
'(Line 4)
'If Matrix.Rows.Count <> Matrix.Columns.Count Or Fraction <> 0 Then
MsgBox "X must be a Square Matrix" & vbNewLine & "N must be an
Integer => 1"
Exit Function
End If
Dim i As Integer
B = Matrix
For i = 1 To N - 1
B = WorksheetFunction.MMult(Matrix, B)
Next i
XtoN = B
End Function
'Thanks for your help