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
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