VBA 数组逆
VBA array inverse
我正在尝试在 Excel VBA 中从电子表格中的方形数组中获取一些值来反转此数组。我有以下代码:
Private Sub CommandButton1_Click()
Dim A As Variant
Dim i As Integer, j As Integer
ReDim A(1 To 3, 1 To 3) As Double
For i = 1 To 3
For j = 1 To 3
A(i, j) = Cells(i, j).Value
Next j
Next i
A = Application.WorksheetFunction.MInverse(A)
End Sub
行中:
A = Application.WorksheetFunction.MInverse(A)
我收到错误:
run-time error 1004: application defined or object defined error
谁能帮我解决这个问题?
您可能正在尝试反转病态矩阵。我在一个简单的示例中尝试了您的代码:
Sub dural()
Dim A As Variant
Dim i As Integer, j As Integer
ReDim A(1 To 3, 1 To 3) As Double
For i = 1 To 3
For j = 1 To 3
A(i, j) = Cells(i, j).Value
Next j
Next i
A = Application.WorksheetFunction.MInverse(A)
For i = 1 To 3
For j = 1 To 3
Cells(i + 5, j + 5).Value = A(i, j)
Next j
Next i
End Sub
并得到:
这似乎是正确的。 (两个矩阵的乘积非常接近一个单位矩阵)
尝试使用下面的代码从单元格 A1
中读取一个 3×3 数组,然后在单元格 A5
上写入逆数组。
Private Sub CommandButton1_Click()
Dim A() as Variant, B() as Variant
A = Range("A1").Resize(3,3).Value
B = WorksheetFunctions.MMinverse(A)
Range("A5").Resize(3,3).Value = B
End Sub
不需要遍历每个单元格,这是一个缓慢的操作。使用 Range().Resize().Value
语法通过一个命令读取和写入整个表。
我正在尝试在 Excel VBA 中从电子表格中的方形数组中获取一些值来反转此数组。我有以下代码:
Private Sub CommandButton1_Click()
Dim A As Variant
Dim i As Integer, j As Integer
ReDim A(1 To 3, 1 To 3) As Double
For i = 1 To 3
For j = 1 To 3
A(i, j) = Cells(i, j).Value
Next j
Next i
A = Application.WorksheetFunction.MInverse(A)
End Sub
行中:
A = Application.WorksheetFunction.MInverse(A)
我收到错误:
run-time error 1004: application defined or object defined error
谁能帮我解决这个问题?
您可能正在尝试反转病态矩阵。我在一个简单的示例中尝试了您的代码:
Sub dural()
Dim A As Variant
Dim i As Integer, j As Integer
ReDim A(1 To 3, 1 To 3) As Double
For i = 1 To 3
For j = 1 To 3
A(i, j) = Cells(i, j).Value
Next j
Next i
A = Application.WorksheetFunction.MInverse(A)
For i = 1 To 3
For j = 1 To 3
Cells(i + 5, j + 5).Value = A(i, j)
Next j
Next i
End Sub
并得到:
这似乎是正确的。 (两个矩阵的乘积非常接近一个单位矩阵)
尝试使用下面的代码从单元格 A1
中读取一个 3×3 数组,然后在单元格 A5
上写入逆数组。
Private Sub CommandButton1_Click()
Dim A() as Variant, B() as Variant
A = Range("A1").Resize(3,3).Value
B = WorksheetFunctions.MMinverse(A)
Range("A5").Resize(3,3).Value = B
End Sub
不需要遍历每个单元格,这是一个缓慢的操作。使用 Range().Resize().Value
语法通过一个命令读取和写入整个表。