1

I am getting the "next without for" error. I checked other questions on this and looked for any open if statements or loops in my code, but could find none. I'm need an extra set of eyes to catch my error here.

I am trying to loop through this code and advance the torque value 3 times each times it gets to the 30th i.

'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
3
  • 4
    That lonely End should be Loop Commented Aug 25, 2017 at 13:29
  • 1
    It's because of your Do While . It should end with a Loop not an End Commented Aug 25, 2017 at 13:30
  • All, the replacing end with a loop worked! Commented Aug 25, 2017 at 13:54

3 Answers 3

2

You are trying to terminate your While with an End instead of Loop

Sign up to request clarification or add additional context in comments.

Comments

1

Try changing your End to Loop in your Do While loop. I think you are terming the loop when you hit that End

Comments

0

Proper indentation makes the problem rather apparent.

You have:

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

But run it through Rubberduck's Smart Indenter and you get:

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

Notice how the End other answers are pointing out, is clearly not delimiting the Do While loop.

The Next i is inside the Do While block, which isn't terminated - when the VBA compiler encounters that Next i, it doesn't know how it could possibly relate to any previously encountered For statement, and thus issues a "Next without For" compile error.

Use an indenter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.