你的end if放錯地方了和next i 換個位置就行了。程式碼如下Sub QR(rng As Range) Dim rng1 As Range, n As Integer Dim q As Integer, p As Integer Dim i As Integer householder rng n = rng.Rows.Count With rng Do q = 1 p = 1 For i = n - 1 To 1 Step -1 If Abs(.Cells(i + 1, i).Value) < 0.000000000001 * (Abs(.Cells(i, i).Value) + Abs(.Cells(i + 1, i + 1).Value)) Then .Cells(i + 1, i).Value = 0 .Cells(i, i + 1).Value = 0 If q > 1 And p = 1 Then p = i + 1 ElseIf q = 1 Then q = i + 1 End If End If Next i If q > 1 Then wilkinsonQR Range(.Cells(p, p), .Cells(q, q)) "呼叫WilkinsonQR步 Loop Until q = 1 End WithEnd Sub
你的end if放錯地方了和next i 換個位置就行了。程式碼如下Sub QR(rng As Range) Dim rng1 As Range, n As Integer Dim q As Integer, p As Integer Dim i As Integer householder rng n = rng.Rows.Count With rng Do q = 1 p = 1 For i = n - 1 To 1 Step -1 If Abs(.Cells(i + 1, i).Value) < 0.000000000001 * (Abs(.Cells(i, i).Value) + Abs(.Cells(i + 1, i + 1).Value)) Then .Cells(i + 1, i).Value = 0 .Cells(i, i + 1).Value = 0 If q > 1 And p = 1 Then p = i + 1 ElseIf q = 1 Then q = i + 1 End If End If Next i If q > 1 Then wilkinsonQR Range(.Cells(p, p), .Cells(q, q)) "呼叫WilkinsonQR步 Loop Until q = 1 End WithEnd Sub