Excelマクロの配列計算が♯VALUE!となる
Excelマクロの複素数を扱う配列計算がエラーとなります.
下記HPからマクロを標準モジュールに取り込み,以前のQAで助けて頂き
一部修正により動作するようになりました.
しかし,このマクロで定義した「 IMINVERS 」関数を実行すると,大きい
配列では「#VALUE!」となってしまいます.
助けて頂けませんか!
http://www.geocities.jp/tomtomf/denki/AC2/ac2.htm
http://www.geocities.jp/tomtomf/denki/AC1/ac1.htm
〔問題の現象〕
整数の行列(6×6)以上の配列計算は「#VALUE!」となる.
複素数の行列(5×5)以上の配列計算は「#VALUE!」となってしまう.
小さい配列では正しく計算できているので,手順には問題ないようにみえます.
どこに問題があるのでしょうか.
問題の「 IMINVERS」関数に関するマクロは以下の通りです。
-----------------------------------------------------------
Public Function IMABSa(a As Variant) As Variant
IMABSa = Application.WorksheetFunction.ImAbs(a)
End Function
Public Function IMDIVa(a As Variant, b As Variant) As Variant
IMDIVa = Application.WorksheetFunction.ImDiv(a, b)
End Function
Public Function IMPRODUCTa(ParamArray a()) As Variant
IMPRODUCTa = Application.WorksheetFunction.ImProduct(a)
End Function
Public Function IMPRODUCTb(a As Variant, b As Variant) As Variant
IMPRODUCTb = Application.WorksheetFunction.ImProduct(a, b)
End Function
Public Function IMSUBa(a As Variant, b As Variant) As Variant
IMSUBa = Application.WorksheetFunction.ImSub(a, b)
End Function
Public Function IMSUMa(ParamArray a()) As Variant
IMSUMa = Application.WorksheetFunction.ImSum(a)
End Function
Public Function IMSUMb(a As Variant) As Variant
IMSUMb = Application.WorksheetFunction.ImSum(a)
End Function
-----------------------------------------------------
Public Function IMINVERS(a As Range) As Variant
Dim n As Integer, n1 As Integer, n2 As Integer
Dim r1 As Integer, r2 As Integer, c As Integer
Dim max As Variant
Dim i As Integer
Dim m() As Variant
Dim inm() As Variant
Dim rr As Integer, cc As Integer
Dim no As Integer, ex As Variant
n1 = a.Rows.Count
n2 = a.Columns.Count
n = n1
ReDim inm(1 To n1, 1 To n2)
For rr = 1 To n1
For cc = 1 To n2
If rr <> cc Then
inm(rr, cc) = 0
Else
inm(rr, cc) = 1
End If
Next
Next
ReDim m(1 To n1, 1 To n2)
m = a
If n1 <> n2 Then
IMINVERS = False
Exit Function
End If
For r1 = 1 To n
max = m(r1, r1)
no = r1
If r1 < n Then
For i = r1 + 1 To n
If IMABSa(m(i, r1)) > IMABSa(max) Then
max = m(i, r1)
no = i
End If
Next
If (r1 <> no) Then
For i = 1 To n
ex = m(r1, i)
m(r1, i) = m(no, i)
m(no, i) = ex
Debug.Print m(r1, i), m(no, i)
ex = inm(r1, i)
inm(r1, i) = inm(no, i)
inm(no, i) = ex
Next
End If
End If
max = m(r1, r1)
For i = 1 To n
m(r1, i) = IMDIVa(m(r1, i), max)
inm(r1, i) = IMDIVa(inm(r1, i), max)
Next
For r2 = 1 To n
If r1 <> r2 Then
max = m(r2, r1)
For i = 1 To n
m(r2, i) = IMSUBa(m(r2, i), IMPRODUCTa(m(r1, i), max))
inm(r2, i) = IMSUBa(inm(r2, i), IMPRODUCTa(inm(r1, i), max))
Next
End If
Next
Next
IMINVERS = inm
End Function
------------------------------------------------
お礼
回答ありがとうございました。上記のHPのとても参考になりました