![]() |
value is changing at the end of Call Sub
I'm not sure if I should be using a func or a sub, but it looks like my
values get passed from one sub to another, and all of the values are OK at the end of the called sub, but my variables "x" and "v" are being reset to 0 when the procedure continues after I call the 2nd sub. My other values are OK. If you want to try this out in Excel, set some values in B1:B4 and B6:B7. I have put in a couple of msgBox's just to see what values the variables hold. Those will be commented out when this is working. Option Explicit ' Sub Worksheet_Change(ByVal Target As Range) ' this will trigger if any part of the sheet is changed Sub One_D_Motion() Dim t As Single Dim t0 As Single Dim time1End As Single Dim time2End As Single Dim x As Single Dim x0 As Integer Dim v0 As Single Dim v As Single Dim acc As Single Dim acc1 As Single Dim acc2 As Single Dim row As Integer ' This will find the position and velcoity for the first 20 seconds ' The user must set the initial conditions and the time period for the accelerations ' Written 6/12/2008 by Jeff Ciaccio ' Clear out the range where the data will go Range("D2:g202").Value = "" ' Record the values the user has chosen x0 = Range("B1").Value v0 = Range("B2").Value acc1 = Range("b3").Value time1End = Range("b4").Value acc2 = Range("b6").Value time2End = Range("B7").Value ' Set the first row cells t = 0 row = 2 Range("D2").Value = 0 Range("E2").Value = x0 Range("f2").Value = v0 Range("G2").Value = acc1 Range("d3").Select Call calc(t, time1End, row, x0, v0, acc1) ' TROUBLE MsgBox "x = " & x & " v = " & v 'MsgBox prompt:="Time period 1: " & vbLf & "The ending position was " & x & " meters, and the starting position was " & x0 & ", so the displacement was " & (x - x0) & " meters", Title:="End of first period" 'MsgBox prompt:="Time period 1: " & vbLf & "The ending velocity was " & v & " m/s, and the starting velocity was " & v0 & ", so the Delta v was " & (v - v0) & " m/s", Title:="End of first period" 'MsgBox ("Time period 1: " & vbLf & "Since there was constant acceleration, the average velocity is:" & vbLf _ & "avg vel = (" & v0 & " + " & v & ") " & Chr(247) & "2 = " & (v0 + v) / 2) & " m/s", Title:="End of first period" Call calc(t, time2End, row, x, v, acc2) 'MsgBox prompt:="Time period 2: " & vbLf & "NOTE: To find the average velocity for the whole period, you CANNOT simply average the starting and ending velocities!" _ & vbLf & "Just find the displacement and divide by 20.0 seconds." & vbLf _ & "The average velocity was " & (x - x0) / 20 & "m/s^2", Title:="End of simulation" End Sub Sub calc(t, timeEnd, row, x, v, acc) Dim t0 As Single Dim x0 As Integer Dim v0 As Single MsgBox "Thanks for passing me t = " & t & " end time = " & timeEnd & "row = " & row & " x = " & x & " v = " & v & " and acc = " & acc t0 = t x0 = x v0 = v Do Until t = timeEnd t = Round(t + 0.1, 1) row = row + 1 x = x0 + v0 * (t - t0) + 1 / 2 * acc * (t - t0) ^ 2 v = v0 + (t - t0) * acc Range("d" & row).Value = t Range("e" & row).Value = x Range("f" & row).Value = v Range("g" & row).Value = acc Loop MsgBox "x = " & x & " v = " & v & " at the end of calc." End Sub -- Jeff Ciaccio Physics and AP Physics Teacher Sprayberry High School; Marietta, GA Blog: http://sprayberry.typepad.com/ciaccio |
value is changing at the end of Call Sub
After the 1st call to Calc, x and v have not been modified, but x0 and v0
were, so x and v should still be 0. At the end of the 2nd Call Calc, I see non-zero values for x and v as they have been passed to and changed by Calc. Perhaps x should be set to x0 and v to v0 after the 1st call to Calc? Also, are x and x0 related? I do see that x0 is declared as Integer and x as Single. Maybe that has something to do with it. Just throwing out a couple of observations as I do not understand what this procedure is doing. -- Tim Zych www.higherdata.com Compare data in worksheets and find differences with Workbook Compare A free, powerful, flexible Excel utility "Jeff Ciaccio" wrote in message ... I'm not sure if I should be using a func or a sub, but it looks like my values get passed from one sub to another, and all of the values are OK at the end of the called sub, but my variables "x" and "v" are being reset to 0 when the procedure continues after I call the 2nd sub. My other values are OK. If you want to try this out in Excel, set some values in B1:B4 and B6:B7. I have put in a couple of msgBox's just to see what values the variables hold. Those will be commented out when this is working. Option Explicit ' Sub Worksheet_Change(ByVal Target As Range) ' this will trigger if any part of the sheet is changed Sub One_D_Motion() Dim t As Single Dim t0 As Single Dim time1End As Single Dim time2End As Single Dim x As Single Dim x0 As Integer Dim v0 As Single Dim v As Single Dim acc As Single Dim acc1 As Single Dim acc2 As Single Dim row As Integer ' This will find the position and velcoity for the first 20 seconds ' The user must set the initial conditions and the time period for the accelerations ' Written 6/12/2008 by Jeff Ciaccio ' Clear out the range where the data will go Range("D2:g202").Value = "" ' Record the values the user has chosen x0 = Range("B1").Value v0 = Range("B2").Value acc1 = Range("b3").Value time1End = Range("b4").Value acc2 = Range("b6").Value time2End = Range("B7").Value ' Set the first row cells t = 0 row = 2 Range("D2").Value = 0 Range("E2").Value = x0 Range("f2").Value = v0 Range("G2").Value = acc1 Range("d3").Select Call calc(t, time1End, row, x0, v0, acc1) ' TROUBLE MsgBox "x = " & x & " v = " & v 'MsgBox prompt:="Time period 1: " & vbLf & "The ending position was " & x & " meters, and the starting position was " & x0 & ", so the displacement was " & (x - x0) & " meters", Title:="End of first period" 'MsgBox prompt:="Time period 1: " & vbLf & "The ending velocity was " & v & " m/s, and the starting velocity was " & v0 & ", so the Delta v was " & (v - v0) & " m/s", Title:="End of first period" 'MsgBox ("Time period 1: " & vbLf & "Since there was constant acceleration, the average velocity is:" & vbLf _ & "avg vel = (" & v0 & " + " & v & ") " & Chr(247) & "2 = " & (v0 + v) / 2) & " m/s", Title:="End of first period" Call calc(t, time2End, row, x, v, acc2) 'MsgBox prompt:="Time period 2: " & vbLf & "NOTE: To find the average velocity for the whole period, you CANNOT simply average the starting and ending velocities!" _ & vbLf & "Just find the displacement and divide by 20.0 seconds." & vbLf _ & "The average velocity was " & (x - x0) / 20 & "m/s^2", Title:="End of simulation" End Sub Sub calc(t, timeEnd, row, x, v, acc) Dim t0 As Single Dim x0 As Integer Dim v0 As Single MsgBox "Thanks for passing me t = " & t & " end time = " & timeEnd & "row = " & row & " x = " & x & " v = " & v & " and acc = " & acc t0 = t x0 = x v0 = v Do Until t = timeEnd t = Round(t + 0.1, 1) row = row + 1 x = x0 + v0 * (t - t0) + 1 / 2 * acc * (t - t0) ^ 2 v = v0 + (t - t0) * acc Range("d" & row).Value = t Range("e" & row).Value = x Range("f" & row).Value = v Range("g" & row).Value = acc Loop MsgBox "x = " & x & " v = " & v & " at the end of calc." End Sub -- Jeff Ciaccio Physics and AP Physics Teacher Sprayberry High School; Marietta, GA Blog: http://sprayberry.typepad.com/ciaccio |
value is changing at the end of Call Sub
Thanks Tim. I changed it a bit to pass x and v instead of x0 and v0. That
did the trick. - Jeff |
All times are GMT +1. The time now is 04:33 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com