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!

runtime error 9: subscript out of range

Status
Not open for further replies.

quayz15

Programmer
Jun 16, 2009
12
US
I am a new programmer, and I am trying to write a code that cycles though an excel spread sheet. Time is in the first column displace in the second and velocity in the third. I would like to process the data one row at a time. I am getting the runtime error when I try to calc acceleration (xddot=...). However, I think the problem stems from the arrays. Is there another way I should be going about this.
thanks in advance.

Private Sub Command1_Click()


Dim s As Integer
Dim xddot As Double
Dim timestep As Integer
Dim z, h As Double
Dim row1, endrow As Integer
Dim time(), x(), xdot() As Long
s = Range(Range("A14"), Range("A65536").End(xlUp)).Count


z = 1
endrow = Range("A65536").End(xlUp).ROW

For timestep = 1 To s
For row1 = 14 To endrow
ReDim time(1 To s, 1), x(1 To s, 2), xdot(1 To s, 3) As Long

xddot = ((xdot(timestep, 3) - xdot(timestep - 1, 3)) / (time(timestep, 1) - time(timestep - 1, 1)))

Dim k1, k2, k3, k4 As Double

Dim i As Long
h = xddot - Cells(row1 - 1, 6).Value

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


Print "when xdot = "; xdot(timestep); "z = "; z
Dim f0 As Double
Dim ft As Double
ft = fc(z, x, xdot, xddot) - f0
Range("E14").Value = ft
Next row1
Next timestep
End Sub
 
This gives me a type mismatch error at the xdot1=range(...). I'm not sure what Im doing wrong. pleas help.
Private Sub Command1_Click()


Dim s As Integer
Dim xddot As Double
Dim timestep As Integer
Dim z, h As Double
Dim row1, endrow As Integer
Dim time, x, xdot As Long
Dim time1, x1, xdot1 As Long
s = Range(Range("A14"), Range("A65536").End(xlUp)).Count


z = 1
x = 0
xdot = 0
xddot = 0
x1 = 0
xdot1 = 0

endrow = Range("A65536").End(xlUp).ROW

For timestep = 1 To s
For row1 = 14 To endrow
time = Range("A14").Offset(timestep - 1, 1).Value
x = Range("B14").Offset(timestep - 1, 2).Value
xdot = Range("C14").Offset(timestep - 1, 3).Value
time1 = Range("A14").Offset(timestep - 2, 1).Value
x1 = Range("B14").Offset(timestep - 2, 2).Value
xdot1 = Range("C14").Offset(timestep - 2, 3).Value

xddot = (xdot - xdot1) / (time - time1)
Range("F14").Offset(timestep - 1, 6).Value = xddot

Dim k1, k2, k3, k4 As Double

Dim i As Long
h = xddot - Cells(row1 - 1, 6).Value

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


Range("G14").Offset(timestep - 1, 7).Value = z
Dim f0 As Double
Dim ft As Double
ft = fc(z, x, xdot, xddot) - f0
Range("E14").Value = ft
Next row1
Next timestep
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top