エクセルマクロで定義した関数が動きません
以前にマクロの記述について教えて頂いた件の続きになります.
ご指導頂いたとおりExcelマクロで複素数を扱う関数を下記HPから
標準モジュールにコピペしました.今度は正しくコピーできたと思いますが,
実行するとエラーになります.
標準の組込み関数を用いて「実数」の行列を計算すれば正しく
計算できますが,当然ながら「複素数」は計算できません.
この「複素数」を扱う新しく定義した関数が動かない理由,
「End if に対するifブロックがありません」とか
計算結果が「#VALUE!」となってしまうのは何故でしょうか?
マクロの記述内容はほとんど理解できないのですが,
どなたか助けて頂けませんか!
ちなみにエクセルは2016版です.
http://www.geocities.jp/tomtomf/denki/AC2/ac2.htm
http://www.geocities.jp/tomtomf/denki/AC1/ac1.htm
以下はコピー定義した「 IMMULT」関数と「 IMINVERS」関数のマクロです.
Public Function IMMULT(a As Range, b As Range) As Variant
Dim r1 As Integer, r2 As Integer, c1 As Integer, c2 As Integer, nn As Integer
Dim r As Integer, c As Integer
Dim cr As Integer, cc As Integer
Dim n As Integer
Dim mm() As Variant
r1 = a.Rows.Count
r2 = b.Rows.Count
c1 = a.Columns.Count
c2 = b.Columns.Count
If (c1 = r2) Then
nn = c1
Else
Exit Function
End If
cr = r1
cc = c2
ReDim mm(1 To cr, 1 To cc)
For r = 1 To cr
For c = 1 To cc
mm(r, c) = 0
For n = 1 To nn
mm(r, c) = IMSUMa(mm(r, c), IMPRODUCTa(a.Cells(r, n), b.Cells(n, c)))
Next
Next
Next
IMMULT = mm
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
お礼
ははは様 お返事遅くなってすみません。 ご回答ありがとうございます。助かりました。 理解できていない部分もございますが、勉強したいと思います。