Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For without next error.

Status
Not open for further replies.

quayz15

Programmer
Jun 16, 2009
12
US
I am trying to teach myself VB and this is my first real program. So, I'm sure this is a simple question. I'm trying to write a program that will process the information given by the manufacturer in a spread sheet, and predict the result. I'm sure that at this point there are many errors in the program but I cant get passed the For without Next error that pops up. I don't understand this because my for loop does have a next statment in it. I'm tring to train myself to think like a programmer so a detailed explanation is appreciated.
Here is what I have so far.

Option Explicit

Private Sub Command1_Click()


Dim x As Range
Dim time As Range
Dim xdot As Range
Dim s As Integer
Dim xddot As Range
Dim timestep As Integer
s = InputBox("Enter Time Steps.")

For timestep = 1 To s
timestep = 1
time = Range("A14").End(xlDown).Offset(1, 0).Select
x = Range("B14").End(xlDown).Offset(1, 0).Select
xdot = Range("C14").End(xlDown).Offset(1, 0).Select
xddot = (xdot - xdot(-1, 0)) / (time - time(-1, 0))
Range("D14").Value = xddot

Dim k1, k2, k3, k4 As Double
Dim z, h As Double
Dim i As Long
z = 1
h = xddot - xddot(-1, 0)

k1 = h * f(xdot, z)
k2 = h * f(xdot + h / 2, z + k1 / 2)
k3 = h * f(xdot + h / 2, z + k2 / 2)
k4 = h * f(xdot + h, z + k3)
z = z + k1 / 6 + k2 / 3 + k3 / 3 + k4 / 6

xdot = xdot + h

Print "when xdot = "; xdot; "z = "; z

End Sub
Function f(xddot, z) As Double
Dim A As Range
Dim n As Range
Dim gamma As Range
Dim betta As Range

If z > 0 And xddot > 0 Then
f = A - (z ^ n) * (gamma + betta)
End If
If z > 0 And xddot < 0 Then
f = A + (z ^ n) * (gamma - betta)
End If
If z < 0 And xddot < 0 Then
f = A - Abs(z ^ n) * (gamma + betta)
End If
If z < 0 And xddot > 0 Then
f = A + Abs(z ^ n) * (gamma - betta)
End If
End Function


Function c(xdot) As Double
Dim a1 As Range
Dim a2 As Range
Dim p As Range
c = a1 * e ^ (-(a2 * Abs(xdot)) ^ p)

End Function

Function fc() As Double
Dim alfa As Range
Dim k As Range
Dim m As Range
fc = alfa * z + k * x + c(xdot) * xdot + m * xddot

End Function
Dim f0 As Double
Dim ft As Double
ft = fc - f0

Cells(timestep + 13, 5) = ft
Next timestep
End Sub

I tried to remove the first End sub but I get an expected end sub error
 
The problem is that you cannot have functions embedded within a function. (this also applies to subroutines).

to cut down the code (for clarification purposes)...



Code:
Private Sub Command1_Click()

Dim ....

[!]For timestep = 1 To s[/!]
  Statements....
End Sub

Function f(xddot, z) As Double
End Function

Function c(xdot) As Double
End Function

Function fc() As Double
End Function

[!]Next timestep[/!]
End Sub

Every For must have a next, but you cannot define functions within the for next loop. You can call functions, but not declare them. Like this....


Code:
Private Sub Command1_Click()

Dim ....

[!]For timestep = 1 To s[/!]
  Statements....
  Use your functions here.
  X = f(Blah)
  y = c(Floo)
  
[!]Next timestep[/!]
End Sub

Function f(xddot, z) As Double
End Function

Function c(xdot) As Double
End Function

Function fc() As Double
End Function


-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 

Also, from your code:
[tt]
s = InputBox("Enter Time Steps.")

For timestep = 1 To s[blue]
timestep = 1[/blue]
....[/tt]

Your variable timestep will always be reset to 1 in your For ... Next loop

Eliminate BLUE code

Have fun.

---- Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top