Excel VBA:“下一个没有”错误

我得到了“下一个没有”的错误。 我检查了这个问题的其他问题,并寻找在我的代码中的任何打开的if语句或循环,但没有find。 我需要一个额外的眼睛来赶上我的错误在这里。

我试图通过这个代码循环,并提高扭矩值3次,每次达到30日我。

'This is Holzer's method for finding the torsional natural frequency Option Explicit Sub TorsionalVibrationAnalysis_() Dim n As Integer 'position along stucture Dim m As Integer Dim i As Long 'frequency to be used Dim j As Variant 'moment of inertia Dim k As Variant 'stiffness Dim theta As Long 'angular displacement Dim torque As ListRow 'torque Dim lambda As Long 'ListRow 'omega^2 Dim w As Variant Dim s As Long 'equations relating the displacement and torque n = 1 Set j = Range("d2:f2").Value 'Range("d2:f2").Value Set k = Range("d3:f3").Value 'initial value Set w = Range("B1:B30").Value For i = 1 To 30 'start at 40 and increment frequency by 20 w = 40 + (i - 1) * 20 lambda = w ^ 2 theta = 1 s = 1 Do While i = 30 & s <= 3 torque = lambda * j(1, s) s = s + 1 End m = n + 1 theta = theta - torque(i, n) / k(n) torque(i, m) = torque(i, n) + lambda * j(m) * theta If m = 4 & i < 30 Then w(i) = 40 + (i - 1) * 20 lambda = w(i) ^ 2 ElseIf m = 4 & i >= 30 Then Cells([d], [5+i]).display (i) Cells([e], [5+i]).display (theta) Cells([f], [5+i]).display (torque) Else End If If m <> 4 Then n = n + 1 End If Next i End Sub 

你正在尝试用End而不是Loop来终止你的While

尝试在Do While循环中更改EndLoop 。 当你击中那个End时,我认为你正在命令循环

正确的缩进使问题变得相当明显。

你有:

 For i = 1 To 30 '... Do While i = 30 & s <= 3 '... End '... If m = 4 & i < 30 Then '... ElseIf m = 4 & i >= 30 Then '... Else End If If m <> 4 Then '... End If Next i 

但是通过Rubberduck的智能压头运行,你会得到:

 For i = 1 To 30 '... Do While i = 30 & s <= 3 '... End '... If m = 4 & i < 30 Then '... ElseIf m = 4 & i >= 30 Then '... Else End If If m <> 4 Then '... End If Next i End Sub 

注意End其他答案是如何指出的,显然不是定界Do While循环。

Next iDo While块里面,这个没有被终止的时候 – 当VBA编译器遇到这个时, Next i不知道它怎么可能和之前遇到的任何For语句相关,于是发出一个“Next without For “编译错误。

使用压头。